spring 注解 @ResponseBody 返回JSON 设置不返回为 null 的值

我会带着你远行 2022-05-27 23:48 665阅读 0赞
  1. 第一种方法,[参考链接](https://segmentfault.com/q/1010000002522525/a-1020000002522849)
  2. 该方法未尝试,进行xml配置时提示错误.不建议使用
  3. <mvc:annotation-driven>
  4. <mvc:message-converters register-defaults="true">
  5. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  6. <property name="objectMapper">
  7. <bean class="com.fasterxml.jackson.databind.ObjectMapper">
  8. <property name="serializationInclusion">
  9. <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
  10. </property>
  11. </bean>
  12. </property>
  13. </bean>
  14. </mvc:message-converters>
  15. </mvc:annotation-driven>
  16. 第二种方法。[参考内容](https://stackoverflow.com/questions/6049523/how-to-configure-spring-mvc-3-to-not-return-null-object-in-json-response#answer-6049967)
  17. 在你要输出的数据的类加上
  18. @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

发表评论

表情:
评论列表 (有 0 条评论,665人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JackSon返回null字段

    说实话这个问题真的让我头疼,最终想到一个投机取巧的方法,下面且听我一一道来。 背景 因为mobile团队为了节省流量,所以架构组就把一些值为`null`或者`""`的字