首先要了解<mvc:annotation-driven/>,请移步至:https://my.oschina.net/HeliosFly/blog/205343
那先看看我的配置:
<!-- 默认的注解映射的支持,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
<!-- 将Jackson2HttpMessageConverter的默认格式化输出为false -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list><value>application/json;charset=UTF-8</value></list>
</property>
<property name="prettyPrint" value="false"/>
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- <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>
<!-- REST中根据URL后缀自动判定Content-Type及相应的View -->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="text/html" />
<property name="ignoreAcceptHeader" value="true"/>
<property name="mediaTypes" >
<map>
<entry key="json" value="application/json;charset=UTF-8"/>
<entry key="xml" value="application/xml"/>
</map>
</property>
<property name="favorPathExtension" value="true"/>
</bean>
content-negotiation-manager:用于多视图的解析
就是 会根据我请求的后缀返回不同的信息,defaultContentType 设置为 text/html,意思就是没有后缀默认返回html,
mediaTypes中定义不同的后缀,返回不同的信息格式,尽量不要设置defaultContentType ,让工具自己选择,不然有可能会产生406错误.