Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object
来源:互联网 发布:windows商城 编辑:程序博客网 时间:2024/05/15 20:02
Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object
资料参考:http://stackoverflow.com/questions/28862483/spring-and-jackson-how-to-disable-fail-on-empty-beans-through-responsebody
出问题前的配置
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json; charset=UTF-8</value> <value>application/x-www-form-urlencoded; charset=UTF-8</value> </list> </property> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> </bean> </property> </bean> </mvc:message-converters></mvc:annotation-driven>
问题描述
SpringMVC的注解@ResponseBody在以下情况使用时:
@ResponseBody@RequestMapping("/your_url/act.do")public Map<String, Object> yourMethod(){ // do your work... Map<String, Object> resultMap = new HashMap<>(); resultMap.put("code", 0); resultMap.put("data", new Object());// 序列化 new Obejct()}
会报如下错误
DefaultHandlerExceptionResolver:134 : Resolving exception from handler []: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) ;
意思是没有找到可用于Object的序列化器,也没有找到属性去创建BeanSerializer。
后面的接着提示了解决方法:(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
解决方案
为SpringMVC里默认序列化使用的 com.fasterxml.jackson.databind.ObjectMapper 设置其属性 SerializationFeature.FAIL_ON_EMPTY_BEANS 为false。
貌似没办法直接在XML配置里设置。只能自定义 com.fasterxml.jackson.databind.ObjectMapper 类进行设置,然后再配置xml。过程如下:
// 继承 ObjectMapper 类public class CustomMapper extends ObjectMapper{ public CustomMapper() { this.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); }}
<!-- Spring MVC 配置 --><mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json; charset=UTF-8</value> <value>application/x-www-form-urlencoded; charset=UTF-8</value> </list> </property> <!-- 配置 objectMapper 为我们自定义扩展后的 CustomMapper --> <property name="objectMapper"> <bean class="com.king.framework.jackson.CustomMapper"> </bean> </property> </bean> </mvc:message-converters></mvc:annotation-driven>
附
关于属性 SerializationFeature.FAIL_ON_EMPTY_BEANS 的更多内容不在本文探讨范围内,详见 Jackson 文档及源码。
1 0
- Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object
- HttpMessageNotWritableException: Could not write JSON: No serializer found for class ****
- SpringBoot中springMVC返回JSON报错-->Could not write JSON: No serializer found for class org.spring...
- Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazy
- Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazy
- Could not write content: No serializer found for class ch.qos.logback.core.spi.LogbackLock
- Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistL
- Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistL
- Axis fault: No serializer found for class
- org.json.JSONObject报错No serializer found for class org.json.JSONObject
- org.codehaus.jackson.map.JsonMappingException: No serializer found for class
- springMVC 使用ajax 出现No serializer found for class异常
- com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class ...
- Spring @Validate 报 :"No validator could be found for type: java.lang.Long" 异常
- 【已解决】java.lang.IllegalArgumentException: No converter found for return value of type: class java.uti
- 解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util
- 解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util
- No validator could be found for type: java.lang.Double
- 深入浅出的理解框架(Struts2、Hibernate、Spring)与 MVC 设计模式
- Hive本地配置方案(内嵌Derby数据库)
- LeetCode 28. Implement strStr()
- iOS开发系列--Objective-C之协议、代码块、分类
- 让你的MacBook不需要插件就能支持NTFS读写
- Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object
- java小技巧之TabelModel;文件选择器;时间属性设置
- 定制EditText的样式
- MD5Helper
- 一 简单工厂模式
- 用深搜和广搜两种方法解题---黑色格子
- BZOJ 1483|HNOI 2009|梦幻布丁|Set
- 一个菜鸡之路
- iOS开发系列—Objective-C之内存管理