1. 程式人生 > >【SpringMVC】SpringMVC全域性配置Json日期型別、為null欄位時不顯示

【SpringMVC】SpringMVC全域性配置Json日期型別、為null欄位時不顯示

 <mvc:annotation-driven>
    	<!-- springMVC整合JSON(配置FastJson)配置 -->
        <mvc:message-converters register-defaults="true">
        	<!-- 配置Fastjson支援 -->
        	<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        		<property name="supportedMediaTypes">
        			<list>
        				<value>text/html;charset=UTF-8</value>
        				<value>application/json</value>
        			</list>
        		</property>
        		<property name="features">
        			<list>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                    </list>
        		</property>
        	</bean>
        	<!-- 配置註解的對映器和介面卡以及其他配置 -->
    		<!-- 處理請求時返回json字串的中文亂碼問題 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
	             <!--避免IE執行AJAX時,返回JSON出現下載檔案 -->
	            <property name="supportedMediaTypes">
		            <list>
		                <value>text/html;charset=UTF-8</value>
		            </list>
		        </property>
	            <property name="objectMapper">
	                <bean class="com.fasterxml.jackson.databind.ObjectMapper">
	                    <!-- 處理responseBody 裡面日期型別 -->
	                    <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>
	                    </property>
	                </bean>
	            </property>
	        </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>