简体   繁体   English

restcontroller和application / x-www-form-urlencoded; charset = UTF-8媒体类型的问题

[英]Issue with restcontroller and application/x-www-form-urlencoded;charset=UTF-8 media type

Task is trivial client sends request using POST with application/x-www-form-urlencoded media type. 任务很简单,客户端使用具有application / x-www-form-urlencoded媒体类型的POST发送请求。 Server receives and response with status OK. 服务器收到状态为OK的响应。 But looks like there is difference between media type application/x-www-form-urlencoded and application/x-www-form-urlencoded;charset=UTF-8 但是,看起来媒体类型application / x-www-form-urlencodedapplication / x-www-form-urlencoded之间是有区别的; charset = UTF-8

Server part: 服务器部分:

@Slf4j
@Controller
@RequestMapping(produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public class CallbackController {

@RequestMapping(value = "/callback/", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public ResponseEntity<?> callback(HttpServletRequest servletRequest, @RequestBody RequestEntity<?> body) throws IOException {
        log.info("Hello: {}", servletRequest);
        log.info("Hello: {}", body);
        String s = extractPostRequestBody(servletRequest);
        log.info("Hello: {}", s);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }
}

Client part: 客户部分:

public void sendcallback() throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8082/callback/";
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        headers.add(HttpHeaders.ACCEPT_CHARSET, "UTF-8");
        headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        headers.add(HttpHeaders.USER_AGENT, "uuuuuuuuuuser");
        headers.add(HttpHeaders.AUTHORIZATION, "none");
MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
        List<String> val1 = new ArrayList<>();
        val1.add("valllll");
        body.put("val", val1);
        HttpEntity<?> entity= new HttpEntity<>(body, headers);
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.POST, entity, Object.class);
        log.info("response: {}", result);
    }

the result output at client side, request: 在客户端输出结果,请求:

"POST /callback/ HTTP/1.1[\r][\n]"
"Content-Type: application/x-www-form-urlencoded[\r][\n]"
"Accept-Charset: UTF-8[\r][\n]"
"Accept: application/x-www-form-urlencoded[\r][\n]"
"User-Agent: uuuuuuuuuuser[\r][\n]"
"Authorization: none[\r][\n]"
"Content-Length: 11[\r][\n]"
"Host: localhost:8082[\r][\n]"
"Connection: Keep-Alive[\r][\n]"
"Accept-Encoding: gzip,deflate[\r][\n]"
"[\r][\n]"
"val=valllll"

response: 响应:

"HTTP/1.1 405 Method Not Allowed[\r][\n]"
"Server: Apache-Coyote/1.1[\r][\n]"
"Allow: GET, HEAD[\r][\n]"
"Content-Language: ru-RU[\r][\n]"
"Content-Length: 0[\r][\n]"
"Date: Tue, 11 Aug 2015 06:28:41 GMT[\r][\n]
"[\r][\n]"

and server in debug mode tell me interesting thing: 服务器处于调试模式告诉我有趣的事情:

在此处输入图片说明

Looks like I have to add FormHttpMessageConverter manually 看起来我必须手动添加FormHttpMessageConverter

@SpringBootApplication
public class CallbackApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(CallbackApplication.class);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FormHttpMessageConverter converter = new FormHttpMessageConverter();
        MediaType mediaType = new MediaType("application","x-www-form-urlencoded", Charset.forName("UTF-8"));
        converter.setSupportedMediaTypes(Arrays.asList(mediaType));
        converters.add(converter);
        super.configureMessageConverters(converters);
    }
}

And don't forget to use correct @RequestBody type 并且不要忘记使用正确的@RequestBody类型

public @ResponseBody ResponseEntity<?> callback(HttpServletRequest servletRequest, @RequestBody MultiValueMap body) { public @ResponseBody ResponseEntity<?> callback(HttpServletRequest servletRequest, @RequestBody MultiValueMap body) {

Since there are: 由于有:

  • org.springframework.util.MultiValueMap org.springframework.util.MultiValueMap
  • org.apache.commons.collections.map.MultiValueMap org.apache.commons.collections.map.MultiValueMap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "status":415, "status":"UNSUPPORTED_MEDIA_TYPE","message":"内容类型 'application/x-www-form-urlencoded;charset=UTF-8' 不支持 - "status":415, "status":"UNSUPPORTED_MEDIA_TYPE","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported Spring Boot - 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8” - Spring Boot - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported HttpMediaTypeNotSupportedException:不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8' - HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 内容类型 'application/x-www-form-urlencoded;charset=UTF-8' 不支持 - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 如何使用application / x-www-form-urlencoded运行jmeter请求; charset = UTF-8“(像邮差的)设置 - How to run jmeter requests with application/x-www-form-urlencoded; charset=UTF-8" (like postman's) setting 如何获得RESTeasy方法来正确使用UTF-8编码的application / x-www-form-urlencode? - How can I get a RESTeasy method to consume application/x-www-form-urlencoded as UTF-8 correctly? 使用Mailgun发送电子邮件-找不到Java类型FormDataMultiPart和MIME媒体类型application / x-www-form-urlencoded的消息正文编写器 - Sending email with Mailgun - a message body writer for Java type FormDataMultiPart and MIME media type application/x-www-form-urlencoded was not found Jersey异常:找不到用于Java类和MIME媒体类型application / x-www-form-urlencoded的消息正文阅读器 - Jersey Exception : A message body reader for Java class and MIME media type application/x-www-form-urlencoded was not found 未找到Form&application / x-www-form-urlencoded的Body Writer - Body Writer not found for Form & application/x-www-form-urlencoded 如何编写控制器类以允许内容类型:application / json和application / x-www-form-urlencoded - How to write controller class to allow content-type: application/json and application/x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM