Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 934
DavidR Avatar asked May 18 '16 15:05

DavidR


1 Answers

Spring 4.2 ContentNegotiatingViewResolver 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>
like image 148
ekem chitsiga Avatar answered Oct 15 '22 18:10

ekem chitsiga