Java 内容类型';application/json';Spring中不支持POST上的MVC
我试图在POST方法中解析一个简单对象,但总是出现以下错误:Java 内容类型';application/json';Spring中不支持POST上的MVC,java,json,spring,spring-mvc,jackson,Java,Json,Spring,Spring Mvc,Jackson,我试图在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 typ
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
在mypom.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>
...
如果我运行CURL:
curl -H "Content-Type: application/json" -X POST -d '{"id":"1","name":"PIZZA","priority":"1","color":"","categoryId":""}' http://localhost:8080/food/uiupdatepost.html
我得到这个错误:
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
HTTP:415
我搜索了所有可能的解决方案,包括:
Category userFromJSON = mapper.readValue("{\"id\":\"1\",\"name\":\"PIZZA\",\"priority\":\"1\",\"color\":\"\",\"categoryId\":\"\"}", Category.class);
Welp。。我想哭。。。在数小时之后,更改: MediaType.APPLICATION\u JSON\u值 为了 MediaType.APPLICATION\u JSON\u UTF8\u值
我的工人。好的。。我想哭。。。在数小时之后,更改: MediaType.APPLICATION\u JSON\u值 为了 MediaType.APPLICATION\u JSON\u UTF8\u值
我的工人。听起来,由于某种原因,Spring无法创建能够解析JSON的适当HTTPMessageConverter。如果jackson在类路径上-正如pom xml中所包含的那样-转换器应该在方法
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.addDefaultHttpMessageConverters
我建议检查一下,为什么没有发生这种情况。或者,您可以手动配置适当的HttpMessageConverter
。如果您有一个从org.springframework.web.servlet.config.annotation.webmvcconfiguer
派生的配置类,您将有如下内容:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(new MappingJackson2XmlHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
}
@覆盖
公共无效配置MessageConverters(列表听起来,Spring由于某种原因无法创建能够解析JSON的适当的HTTPMessageConverter。如果jackson在类路径上-正如pom xml中所包含的那样-转换器应该在方法org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.AddDefaultHttpMessageConvert中注册。)RTER
我建议您检查一下,为什么不会发生这种情况。或者,您可以手动配置适当的HttpMessageConverter
。如果您有一个从org.springframework.web.servlet.config.annotation.webmvcconfiguer
派生的配置类,那么您将有如下内容:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(new MappingJackson2XmlHttpMessageConverter());
converters.add(new MappingJackson2HttpMessageConverter());
}
@覆盖
public void configureMessageConverters(在您的控制器类上方使用@RestController。在您的控制器类上方使用@RestController。谢谢!这帮助我找到了解决方案:我缺少@EnableWebMvc。很高兴帮助您;-)谢谢!这帮助我找到了解决方案:我缺少@EnableWebMvc。很高兴帮助您;-)