不支持内容类型空白

编程入门 行业动态 更新时间:2024-10-22 16:26:08
本文介绍了不支持内容类型空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在 content-type 为空时处理 POST 请求.

I want to handle the POST request when there is empty content-type.

  • 当我添加消耗 = MediaType.APPLICATION_JSON_VALUE并在内容类型为空白的邮递员中提出请求,我收到以下错误
  • { "timestamp": 1581594986909, "status": 415, "error": "Unsupported Media Type", "message": "Content type '' not supported", "path": "/test" }

    这是代码

    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity create(@RequestBody TestRequest testRequest) throws TestException { LOG.debug("Starting..."); //code return createtest(testRequest); }

  • 当我删除消耗 = MediaType.APPLICATION_JSON_VALUE并使用 content-type = blank 发出请求我收到以下错误
  • { "timestamp": 1581595348209, "status": 415, "error": "Unsupported Media Type", "message": "Content type 'application/octet-stream' not supported", "path": "/test" }

    这是代码

    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity create(@RequestBody TestRequest testRequest) throws TestException { LOG.debug("Starting..."); //code return createtest(testRequest); }

    这是邮递员请求

    我想处理这种情况并假设发送了 content-Type= application/json

    I want to handle this scenario and assume as if content-Type= application/json is sent

    推荐答案

    我终于配置了它并且它正在工作.这是 MappingJackson2HttpMessageConverter 的正确配置

    I finally configured it and it is working. Here is the correct configuration for MappingJackson2HttpMessageConverter

    @Configuration(proxyBeanMethods = false) public class WebMvcConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(jacksonMessageConverter()); WebMvcConfigurer.super.configureMessageConverters(converters); } @Bean public MappingJackson2HttpMessageConverter jacksonMessageConverter() { MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter(); List<MediaType> supportedMediaTypes=new ArrayList<>(); supportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); messageConverter.setSupportedMediaTypes(supportedMediaTypes); supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM); messageConverter.setSupportedMediaTypes(supportedMediaTypes); messageConverter.setPrettyPrint(true); return messageConverter; }

    在你想要支持八位字节流的控制器方法中添加 APPLICATION_OCTET_STREAM_VALUE }.

    Aso add the APPLICATION_OCTET_STREAM_VALUE } in the controller method you want to support the octet-stream.

    consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE }

    更多推荐

    不支持内容类型空白

    本文发布于:2023-10-29 10:05:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1539413.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:不支持   空白   类型   内容

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!