콘텐츠 유형 'application/x-www-form-urlencoded;charset=UTF-8'은 @RequestBody MultiValueMap에서 지원되지 않습니다.
Spring @Controller로 인코딩된 x-www-form-url 문제에 대한 답변을 기반으로 합니다.
아래 @Controller 메서드를 작성했습니다.
@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
, produces = {"application/json", "application/xml"}
, consumes = {"application/x-www-form-urlencoded"}
)
public
@ResponseBody
Representation authenticate(@PathVariable("email") String anEmailAddress,
@RequestBody MultiValueMap paramMap)
throws Exception {
if(paramMap == null || paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
}
다음 오류로 인해 실패한 요청
{
"timestamp": 1447911866786,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/users/usermail%40gmail.com/authenticate"
}
[PS: Jersey가 훨씬 더 친절했지만, 현실적 제약으로 인해 지금은 사용할 수 없었다]
문제는 애플리케이션/x-www-form-urlencoded를 사용할 때 Spring이 RequestBody로 인식하지 못한다는 것입니다.따라서 이를 사용하려면 @RequestBody 주석을 삭제해야 합니다.
다음에, 다음을 시험해 주세요.
@RequestMapping(
path = "/{email}/authenticate",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = {
MediaType.APPLICATION_ATOM_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE
})
public @ResponseBody Representation authenticate(
@PathVariable("email") String anEmailAddress,
MultiValueMap paramMap) throws Exception {
if (paramMap == null &&
paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
return null;
}
는 주석 @RequestBody 를 삭제하였습니다.
답변: 콘텐츠타입 어플리케이션/x-www-form-urlencoded를 사용한Http Post 요구가 봄에 기능하지 않습니다.
에 ''을 붙이면 것 요.@RequestParam
그러면 효과가 있을 거야
@PostMapping( "some/request/path" )
public void someControllerMethod( @RequestParam Map<String, String> body ) {
//work with Map
}
요청에 헤더를 추가하여 콘텐츠 유형을 응용 프로그램/json으로 설정합니다.
curl -H 'Content-Type: application/json' -s -XPOST http://your.domain.com/ -d YOUR_JSON_BODY
이런 식으로 봄은 내용을 해석하는 방법을 알고 있다.
봄 5에
@PostMapping( "some/request/path" )
public void someControllerMethod( @RequestParam MultiValueMap body ) {
// import org.springframework.util.MultiValueMap;
String datax = (String) body .getFirst("datax");
}
@RequestBody MultiValueMap paramMap
여기서 @RequestBody Annotaion을 삭제합니다.
@RequestMapping(value = "/signin",method = RequestMethod.POST)
public String createAccount(@RequestBody LogingData user){
logingService.save(user);
return "login";
}
@RequestMapping(value = "/signin",method = RequestMethod.POST)
public String createAccount( LogingData user){
logingService.save(user);
return "login";
}
이런 거죠.
「」를 삭제하기만 .@RequestBody
스프링 부츠 2
@RestController
public class MyController {
@PostMapping
public void method(@Valid RequestDto dto) {
// method body ...
}
}
Spring MVC에서 간단한 HTML 폼 제출(타임 리프나 Spring 폼 태그 사용 안 함)을 처리할 때도 같은 문제가 발생하였습니다.
Douglas Ribeiro의 답변은 매우 효과적일 것입니다.봄 MVC에서 '@RequestBody'를 정말 쓰고 싶은 분들을 위해서.
문제의 원인은 다음과 같습니다.
- 스프링은 "Content-Type"을 인식하고 "Content-Type"을 메서드 서명에서 선언한 파라미터 유형으로 변환해야 합니다.
- application/x-www-form-urlencoded'는 지원되지 않습니다.이는 기본적으로 스프링이 변환 작업(단계 2)을 수행하기 위한 적절한 HttpMessageConverter를 찾을 수 없기 때문입니다.
솔루션:
- 적절한 Http Message Converter를 Spring 어플리케이션 설정에 수동으로 추가합니다.
순서:
- 사용할 Http Message Converter 클래스를 선택합니다.application/x-www-form-urlencoded'에는 org.springframework.http.converter를 선택할 수 있습니다.FormHttpMessageConverter"를 참조해 주세요.
- "public void configure Message Converters"(목록<Http Message Converter?)를 호출하여 FormHttp Message Converter 개체를 Spring 설정에 추가합니다.>> converters)" 메서드를 사용합니다.메서드 내에서는 필요에 따라 "converters.add()"를 사용하여 임의의 Http Message Converter 개체를 추가할 수 있습니다.
그런데 "@RequestParam"을 사용하여 값에 액세스할 수 있는 이유는 다음과 같습니다.
서블릿 사양(섹션 3.1.1)에 따라:
다음은 포스트 폼 데이터가 매개 변수 세트에 입력되기 전에 충족해야 하는 조건입니다.요청은 HTTP 또는 HTTPS 요청입니다.2 .HTTP 메서드는 POST입니다.3. 콘텐츠 타입은 application/x-www-form-urlencoded. 4.서블릿은 요청 오브젝트 상의 getParameter 메서드패밀리의 어느 쪽인가에 대해 첫 번째 콜을 발신했습니다.
따라서 요청 본문의 값이 파라미터로 채워집니다.단, 봄에는 RequestBody에 접속할 수 있습니다.같은 메서드의 시그니처로 @RequestBody와 @RequestParam을 사용할 수도 있습니다.예를 들어 다음과 같습니다.
@RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public String processForm(@RequestParam Map<String, String> inputValue, @RequestBody MultiValueMap<String, List<String>> formInfo) {
......
......
}
inputValue 및 formInfo에는 동일한 데이터가 포함되어 있습니다.「@RequestParam」유형의 expet은 Map, 「@RequestBody」유형은 MultiValueMap입니다.
이 StackOverflow 답변에는 대체 방법에 대해 기재되어 있습니다.
거기서 나는 코드로 설명하면서 차근차근 썼다.간단한 방법:
첫 번째: 객체 쓰기
두 번째: AbstractHttpMessageConverter를 확장하는 모델을 매핑하는 컨버터를 만듭니다.
세 번째: configureMessageConverters 메서드를 덮어쓰고 WebMvcConfigr.class를 구현하는 이 컨버터를 봄용으로 사용하도록 지시합니다.
네 번째이자 마지막: 는 컨트롤러 내부의 매핑에서 이 구현 설정을 사용하여 = MediaType을 소비합니다.APPLICATION_FORM_URLENCODED_VALUE 및 @RequestBody를 오브젝트 앞에 배치합니다.
스프링 부츠 2를 쓰고 있어요.
@PostMapping(path = "/my/endpoint", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public ResponseEntity<Void> handleBrowserSubmissions(MyDTO dto) throws Exception {
...
}
그렇게 하면 된다
스프링 컨버터의 서포트를 켜볼 수 있습니다.
@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// add converter suport Content-Type: 'application/x-www-form-urlencoded'
converters.stream()
.filter(AllEncompassingFormHttpMessageConverter.class::isInstance)
.map(AllEncompassingFormHttpMessageConverter.class::cast)
.findFirst()
.ifPresent(converter -> converter.addSupportedMediaTypes(MediaType.APPLICATION_FORM_URLENCODED_VALUE));
}
}
JMeter를 사용하여 테스트하는 경우 HTTP Header Manager를 추가합니다.
언급URL : https://stackoverflow.com/questions/33796218/content-type-application-x-www-form-urlencodedcharset-utf-8-not-supported-for
'programing' 카테고리의 다른 글
전송 시 JQuery가 빈 어레이를 삭제합니다. (0) | 2023.03.13 |
---|---|
컨트롤러가 필요한 테스트 지침 (0) | 2023.03.13 |
Google-map-react에서 Google 지도에 마커 추가 (0) | 2023.03.08 |
Spring Boot/Spring Security에서 사용자가 자신의 데이터에만 접근할 수 있도록 하는 방법 (0) | 2023.03.08 |
사용자 지정 분류법이 Post Gutenberg 편집기에 표시되지 않음 (0) | 2023.03.08 |