SpringMVC返回JSON,为空时不返回的配置一直报错

猫鼠同眠 2015-10-14 12:07:44
SpringMVC配置JSON转换器,如果字段为空,则不输出,配置如下:
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 时间格式转换 -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
<!-- 为null字段时不转换 -->
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
<!-- <bean class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField" value="com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL" />
</bean> -->
</property>
</bean>
</property>
</bean>


但是启动时,一直报错,错误如下:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'serializationInclusion' of bean class [com.fasterxml.jackson.databind.ObjectMapper]: Bean property 'serializationInclusion' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 61 more
...全文
497 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
猫鼠同眠 2015-10-14
  • 打赏
  • 举报
回复
引用 1 楼 u012807344 的回复:
serializationInclusion 这个不是有问题么?~
现在就是不知道为什么有问题,com.fasterxml.jackson.databind.ObjectMapper类中是有setSerializationInclusion()方法的
猫鼠同眠 2015-10-14
  • 打赏
  • 举报
回复
引用 2 楼 like987654 的回复:
在需要序列化为json输出的类上增加 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
这样只能对单个类起作用,如果需要序列化的类多了好麻烦的
like987654 2015-10-14
  • 打赏
  • 举报
回复
在需要序列化为json输出的类上增加 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
今天的角色00 2015-10-14
  • 打赏
  • 举报
回复
serializationInclusion 这个不是有问题么?~

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧