我得到了著名的Json反序列化异常与spring4.1.0.发布:
org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application/json;charset=UTF-8'不支持
只是在经历了几个类似的线程之后,我仍然无法找到问题的原因
这是我的控制器:
@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String myMethod( @RequestBody MyObj request) {
.....
}
POJO:
public class MyObj implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
Integer something;
Integer[] somethingElse;
// Getters and setters
}
我确实有杰克逊依赖症:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.6</version>
</dependency>
在我的POM 中
我发出以下请求:
{"某物":4,"某事":[1,2]}
我检查了我是否有一个格式正确的JSON,并且它击中了我服务器上合适的控制器。
现在可能出了什么问题????
编辑:此配置更改修复了问题:
在我的根配置中
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application" />
<constructor-arg index="1" value="json" />
<constructor-arg index="2" value="UTF-8" />
</bean>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
在servlet配置中
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<beans:ref bean="jacksonMessageConverter"></beans:ref>
</mvc:message-converters>
</mvc:annotation-driven>
在POM中,添加
的最新版本jackson mapper asl,jackson核心,jackson数据绑定,jackson注释
感谢大家
您是否禁用了默认的MessageConverter?在您的配置中,似乎缺少MappingJackson2MessageConverter。
你能提供你的mvc配置吗?