DavidR DavidR - 2 months ago 37
Java Question

ContentNegotiatingViewResolver MediaTypes Error after Upgrading to Spring 4.2

I have a dispatcher-servlet.xml file that has a

Bean
like this:

<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>


After upgrading to Spring 4.2 I get the following error:

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]


This worked before the upgrade, so any help on how to fix would be greatly appreciated!

Answer

Spring 4.2 ContentNegotiationManager now delegates content negotiation to a ContentNegotiationManager. Define a ContentNegotiationManager bean in your application context and set it on your view resolver as follows

 <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>

Then update your view resolver as follows

<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>