美文网首页
spring配置controller返回的json数据不显示nu

spring配置controller返回的json数据不显示nu

作者: 至爱雅鸿_e631 | 来源:发表于2019-11-21 19:07 被阅读0次

mark

今天(2019年11月21日19:01:10)遇到了一个蛋疼的问题,服务发布的是第三方下发的jar包,之前结果返回中没有为null的属性,今天发版后突然多出了null属性,导致业务方验签失败,然后就开始了蛋疼的排查之旅

解决方法

咱这不是casestudy,所以就直接上解决方法了
由于不能动源码,我能修改的只有配置文件,使用的方法是修改mvc.xml配置,使之过滤对象中为null的属性
直接添加一下配置就行,缺少的命名空间自己复制

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:beans="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <beans:property name="objectMapper">
                    <beans:bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <beans:property name="serializationInclusion">
                            <util:constant
                                static-field="com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL" />
                        </beans:property>
                    </beans:bean>
                </beans:property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

seo优化
Spring4 MVC 返回json格式时候 设置不返回null值属性的问题

相关文章

网友评论

      本文标题:spring配置controller返回的json数据不显示nu

      本文链接:https://www.haomeiwen.com/subject/jnztwctx.html