springMVC3.2 与 springMVC4.3配置mediaTypes的不同之处

springMVC3.2 与 springMVC4.3配置mediaTypes的不同之处

 

替换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版本上已经进行了移出操作。

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的相关方法 
这里写图片描述

转载于:https://my.oschina.net/spinachgit/blog/1796749

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值