本文共 2024 字,大约阅读时间需要 6 分钟。
ERROR:Bean property 'mediaTypes' is not writable or has an invalid setter method. Does theparameter type of the setter match the return type of the getter?后续spring版本中的mediaTypes居然不支持setter方法了
(解决方法亲测有用)
一般的为了使用Json,对其配置:
结果是会出现上述错误提示:Bean property'mediaTypes' is not writable or has an invalid setter method. Does theparameter type of the setter match the return type of the getter?
意思是ContentNegotiatingViewResolver中不再支持mediaTypes的setter方法了。究其原因,是因为ContentNegotiatingViewResolver的setMediaTypes(Map<String,String>mediaTypes)方法已经被弃用(deprecated),不仅如此,一同被deprecated的的还有setFavorPathEctension 、setFavorParameter、setIgnoreAcceptHeader等方法,但是,这些方法都转移到了ContentNegotiationManager中了,为此,要想对其使用,可以转换为对ContentNegotiationManager的配置。
另外,对于数据形式的多种形式表述(html/xml/json/pdf/excel),Spring有两种表述形式:
・ org.springframework.web.servlet.view.ContentNegotiatingViewResolver
・ org.springframework.http.converter.HttpMessageConverte
ContentNegotiatingViewResolver虽然是ViewResolver一族的,但是并不解析视图,而是将其委托给其他的ViewResolver。
介于spring的两种表述方法,但是ContentNegotiatingViewResolver弃用了setMediaTypes,所以用contentNegotiationManager替换,但是配置contentNegotiationManager是通过ContentNegotiationManagerFactoryBean来实现的,所以,选择第一种办法,要对ContentNegotiationManagerFactoryBean进行配置。
所以,在这里,呈现不同格式的数据可以有两种办法:ContentNegotiationManager和HttpMessageConverter
首先在pom.xml文件中加入依赖,然后对其选择一种配置方案即可成功,亲测有用。
com.fasterxml.jackson.core jackson-databind 2.5.4
text/html;charset=UTF-8 application/json;charset=UTF-8
友情链接:
转载地址:http://odlrb.baihongyu.com/