欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring3升级Spring4(maven) springmavenmvc 

程序员文章站 2024-02-05 17:54:22
...
1、升级Spring jar版本3.X-4.X

2、升级spring xmlns 版本由3.X.xsd升级到4.X.xsd

3、多视图
org.springframework.http.converter.json.MappingJacksonHttpMessageConverter升级org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
	<bean class="com.fasterxml.jackson.databind.ObjectMapper" id="objectMapper">
		<!-- 处理responseBody 里面日期类型 -->
		<property name="dateFormat">
			<bean class="java.text.SimpleDateFormat">
				<constructor-arg index="0" type="java.lang.String"
					value="yyyy-MM-dd HH:mm:ss" />
			</bean>
		</property>
		<property name="serializationInclusion">
			<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_EMPTY</value>
		</property>
	</bean>
	<mvc:annotation-driven>
		<mvc:message-converters>
			<bean
				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>
				<property name="objectMapper">
					<ref bean="objectMapper" />
				</property>
			</bean>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<!--避免出现乱码 -->
				<constructor-arg value="UTF-8" index="0"></constructor-arg>
				<property name="supportedMediaTypes">
					<list>
						<value>text/plain;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>


4、org.springframework.web.servlet.view.ContentNegotiatingViewResolver属性defaultViews 由org.springframework.web.servlet.view.json.MappingJacksonJsonView变更org.springframework.web.servlet.view.json.MappingJackson2JsonView;
以前属性:
		<property name="ignoreAcceptHeader" value="true" />
		<property name="defaultContentType" value="text/html" />
		<property name="mediaTypes">
			<map>
				<entry key="json" value="application/json;charset=UTF-8" />
				<entry key="xml" value="application/xml;charset=UTF-8" />
			</map>
		</property>
		<property name="favorParameter" value="false" />

变更
   <bean id="contentNegotiationManager"  class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="true" />
        <property name="ignoreAcceptHeader" value="true"/>
        <property name="useJaf" value="false"/>
        <property name="defaultContentType" value="text/html" />
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json;charset=UTF-8" />
				<entry key="xml" value="application/xml;charset=UTF-8" />
           </map>
        </property>
    </bean>


	<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
		<property name="viewResolvers">
.................................


相关标签: spring maven mvc