升级到Spring 4.2后ContentNegotiatingViewResolver MediaTypes错误

时间:2016-05-18 15:30:17

标签: java spring spring-mvc

我有一个dispatcher-servlet.xml文件,其Bean如下:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:defaultContentType="text/html">
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html"/>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean id="localBasedViewResolver" class="com.company.web.i18n.LocaleBasedViewResolver"
                  p:viewClass="org.springframework.web.servlet.view.JstlView"
                  p:prefix="${views.prefix}"
                  p:suffix="${views.suffix}"
                  p:cacheUnresolved="false"
                  p:exposedContextBeanNames="webProperties"
                  p:order="1"
                  />
            <bean id="viewResolver"
                  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                  p:viewClass="org.springframework.web.servlet.view.JstlView"
                  p:prefix="${views.prefix}"
                  p:suffix="${views.suffix}"
                  p:cacheUnresolved="false"
                  p:exposedContextBeanNames="webProperties"
                  p:order="2"
                  />
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
        </list>
    </property>
</bean>

升级到Spring 4.2后,我收到以下错误:

Caused by: 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?
    at org.springframework.beans.BeanWrapperImpl.createNotWritablePropertyException(BeanWrapperImpl.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:423) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]

这在升级之前有效,所以非常感谢任何有关如何修复的帮助!

1 个答案:

答案 0 :(得分:14)

Spring 4.2 ContentNegotiatingViewResolver现在将内容协商委托给ContentNegotiationManager。在应用程序上下文中定义ContentNegotiationManager bean,并在视图解析器上将其设置如下

 <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"/>
           </map>
        </property>
    </bean>

然后按如下所示更新您的视图解析器

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
    <property name="viewResolvers">
        <list>
            <bean id="localBasedViewResolver" class="com.company.web.i18n.LocaleBasedViewResolver"
                  p:viewClass="org.springframework.web.servlet.view.JstlView"
                  p:prefix="${views.prefix}"
                  p:suffix="${views.suffix}"
                  p:cacheUnresolved="false"
                  p:exposedContextBeanNames="webProperties"
                  p:order="1"
                  />
            <bean id="viewResolver"
                  class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                  p:viewClass="org.springframework.web.servlet.view.JstlView"
                  p:prefix="${views.prefix}"
                  p:suffix="${views.suffix}"
                  p:cacheUnresolved="false"
                  p:exposedContextBeanNames="webProperties"
                  p:order="2"
                  />
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>
        </list>
    </property>
</bean>