不支持内容类型空白 [英] Content type blank is not supported

查看:91
本文介绍了不支持内容类型空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 当我添加消耗 = 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);
    }

  1. 当我删除消耗 = 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
 }

这篇关于不支持内容类型空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆