本文介绍了不支持内容类型空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我添加消耗 = MediaType.APPLICATION_JSON_VALUE并在内容类型为空白的邮递员中提出请求,我收到以下错误
{
"timestamp": 1581594986909,
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type '' not supported",
"path": "/test"
}
当我删除消耗 = 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"
}
我想在 content-type 为空时处理 POST 请求.
I want to handle the POST request when there is empty content-type.
这是代码
@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); }这是代码
@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 }更多推荐
不支持内容类型空白
发布评论