springMVC @responseBody返回对象时过滤空值,日期的处理

过滤空值:

方法一:springMVC配置文件中加相关配置:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true"> 
	<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <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>

方法二:在相关方法上加上注解:@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

过滤空值与转换日期格式:

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true"> 
	<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <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>
                    <!-- 默认日期转换为时间戳,此处配置将日期按指定格式转换 -->
                    <property name="dateFormat">
                    	<bean class="java.text.SimpleDateFormat">
                    		<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
                    	</bean>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页