`

spring 出错,Could not find acceptable representation

 
阅读更多
原文地址:http://www.myexception.cn/program/1632922.html


spring 报错,Could not find acceptable representation
使用spring mvc框架我们可以可以使用@ResponseBody注解,相应ajax请求并返回一个对象,如我们自定义的对象,或者一个List,但这需要我们在spring的配置文件中进行配置:
否则回报:org.springframework.web.HttpMediaTypeNotAcceptableException:Could not find acceptable representation 错误

下面是配置代码:
<!--处理 @ResponseBody 中文乱码问题-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>
text/html;charset=UTF-8
</value>
</list>
</property>
</bean>
<!-- 启动JSON格式的配置 -->
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation -->
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>

另外我们还需要注意对用到的jar进行引用,如果报 java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonGenerationException
错误应该是缺少jackson相关的jar
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics