[英]Content type 'application/json' not supported in Spring MVC on POST
im trying to parse a simple object in a POST method but I'm always getting this error: 我试图在POST方法中解析一个简单的对象,但我总是收到此错误:
2017-02-23 03:52:45 DEBUG AnnotationMethodHandlerExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG ResponseStatusExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
2017-02-23 03:52:45 DEBUG DefaultHandlerExceptionResolver:133 - Resolving exception from handler [com.dlo.food.controller.Lists@40079d7e]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
In my pom.xml I've included the jackson library files: 在我的pom.xml中,我包含了杰克逊库文件:
...
<properties>
<jackson.version>2.6.3</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
...
And I've declared the controller this way: 我以这种方式声明了控制器:
/**
*
* @param request
* @return
* @return
*/
@ResponseBody
@RequestMapping(value = "/uiupdatepost", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> UIUpdatePost(@RequestBody Category category, @RequestHeader HttpServletRequest request) {
try {
return ResponseEntity.ok("");
} catch (Throwable t) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new UIError(t).toHTML());
}
}
The class category is very simple: 类类别非常简单:
public class Category implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public Category() {
//
}
private String id;
private String name;
private String priority;
private String categoryId;
private String color;
/**
ALL GETTER AND SETTERS
**/
}
If I run CURL: 如果我运行CURL:
curl -H "Content-Type: application/json" -X POST -d '{"id":"1","name":"PIZZA","priority":"1","color":"","categoryId":""}' http://localhost:8080/food/uiupdatepost.html
I get this error: 我收到此错误:
HTTP: 415 HTTP:415
I've searched all the possible solutions included: 我搜索了所有可能的解决方案,包括:
Also tested the manual deserialization without problems: 还测试了手动反序列化没有问题:
Category userFromJSON = mapper.readValue("{\"id\":\"1\",\"name\":\"PIZZA\",\"priority\":\"1\",\"color\":\"\",\"categoryId\":\"\"}", Category.class);
Sounds, that Spring wasn't able to create an appropriate HTTPMessageConverter able to parse JSON for some reason. 听起来,由于某种原因,Spring无法创建能够解析JSON的适当HTTPMessageConverter。 If jackson is on the classpath - as it is as contained in your pom-xml - the converter should be registered in method
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters
如果jackson在类路径上(如pom-xml中所包含的那样),则转换器应在
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters
方法中注册
I'd suggest to check, why this isn't happening. 我建议检查一下,为什么不发生这种情况。 Alternatively you can configure appropriate
HttpMessageConverter
by hand. 另外,您可以手动配置适当的
HttpMessageConverter
。 If you've a configuration class derived from org.springframework.web.servlet.config.annotation.WebMvcConfigurer
you would have something like this: 如果您有一个从
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
派生的配置类,则将具有以下内容:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(new MappingJackson2XmlHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
}
Welp.. I want to cry... after hours and hours, changing : Welp ..我想哭……几个小时后,改变:
MediaType.APPLICATION_JSON_VALUE MediaType.APPLICATION_JSON_VALUE
for 对于
MediaType.APPLICATION_JSON_UTF8_VALUE MediaType.APPLICATION_JSON_UTF8_VALUE
worker for me. 我的工人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.