Spring MVC 解读,

首先要了解<mvc:annotation-driven/>,请移步至:https://my.oschina.net/HeliosFly/blog/205343html

 

那先看看个人配置:java

<!-- 默认的注解映射的支持,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:用于多视图的解析ios

就是 会根据我请求的后缀返回不一样的信息,defaultContentType 设置为 text/html,意思就是没有后缀默认返回html,web

mediaTypes中定义不一样的后缀,返回不一样的信息格式,尽可能不要设置defaultContentType ,让工具本身选择,否则有可能会产生406错误.spring

相关文章
相关标签/搜索