---
title: "spring4-json.xml"
author: "chokee"
date: "2017-04-24T17:45:27Z"
tags: []
categories: ["编程语言"]
slug: "spring4-json-xml"
draft: false
lastmod: "2017-04-24T17:45:27Z"
id: 2371030
url: "https://chokee.iteye.com/blog/2371030"
---

<?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd    
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd    
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd    
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd    
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd    
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd    
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"
>
	<context:component-scan base-package="com.kge" />
	<cache:annotation-driven cache-manager="cacheManager" />
	<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true">
		<property name="configLocation" value="classpath:ehcache-setting.xml" />
	</bean>
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
		<property name="cacheManager" ref="cacheManagerFactory" />
	</bean>
	<!-- -->
	<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="order" value="-1" />
		<property name="messageConverters">
			<list>
				<!-- <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> -->
				<ref bean="mappingJacksonHttpMessageConverter" />
			</list>
		</property>
		<property name="webBindingInitializer">
			<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
				<property name="conversionService">
					<!-- 针对普通请求(非application/json) 前台的日期字符串与后台的Java Date对象转化, 此情况,应使用spring mvc本身的内置日期处理 -->
					<!-- 可以在VO属性上加注解：@DateTimeFormat 需要类库joda-time -->
					<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
					</bean>
				</property>
			</bean>
		</property>
	</bean>
	<!-- json请求(application/json)返回值Date转String，全局配置 -->
	<bean name="jacksonObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
		<property name="featuresToDisable">
			<array>
				<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
			</array>
		</property>
		<!-- 如果想自定义，可以在VO属性上加注解：@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = Consts.DATE_PATTERN.DATE_PATTERN_OBLIQUE,timezone = "GMT+8") -->
		<property name="simpleDateFormat">
			<value>yyyy-MM-dd HH:mm:ss</value>
		</property>
	</bean>
	<!--避免IE执行Ajax时，返回JSON出现下载文件 -->
	<!-- 自定义 -->
	<bean id="mappingJacksonHttpMessageConverter" class="com.kge.service.oauth.oltu.control.CustomJackson2HttpMessageConverter">
		<property name="objectMapper" ref="jacksonObjectMapper" />
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	<mvc:annotation-driven />
	<mvc:default-servlet-handler />
	<!-- -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>