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

相關文章
相關標籤/搜索