springMVC3.2 與 springMVC4.3配置mediaTypes的不一樣之處

springMVC3.2 與 springMVC4.3配置mediaTypes的不一樣之處html

 

替換springMVC版本爲4.3之後運行項目提示錯誤

org.springframework.beans.NotWritablePropertyException: Invalid property 'mediaTypes' of bean class [org.springframework.web.servlet.view.ContentNegotiatingViewResolver]: Bean property 'mediaTypes' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter

錯誤緣由

是spring3.2以上開始不推薦使用setMediaTypes等直接設置這些數據, 而是經過ContentNegotiationManager(ContentNegotiationManagerFactoryBean),因此採用map標籤的方式直接轉換會出現異常信息。  
相關的方法在4.3版本上已經進行了移出操做。web

3.2xml配置

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="mediaTypes">
            <map>
                <entry key="html"  value="text/html" />
                <entry key="xml"  value="application/html" />
                <entry key="json" value="application/json" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                </bean>
            </list>
        </property>
        <property name="ignoreAcceptHeader" value="true" />
    </bean>

4.3xml配置

<bean id="contentNegotiationManager"  class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
       <property name="favorParameter" value="true"/>
       <property name="parameterName" value="format"/>
       <property name="ignoreAcceptHeader" value="false"/>
       <property name="mediaTypes">
           <value>
                json=application/json
                xml=application/xml
                html=text/html
            </value>
       </property>
       <property name="defaultContentType" value="text/html"/>
    </bean>

ContentNegotiationManagerFactoryBean關於MediaType的相關方法 
這裏寫圖片描述spring

相關文章
相關標籤/搜索