spring配置错误Could not find acceptable representation

来源:互联网 发布:免费售楼软件 编辑:程序博客网 时间:2024/05/01 01:02
pring 报错,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><span style="color:#ff0000;background-color: #ffffff;"><!-- 启动JSON格式的配置 --><bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation --><property name="supportedMediaTypes"><list><value><span style="background-color: #99ccff;">application/json;charset=UTF-8</span></value></list></property></bean></span></list></property></bean>

 

另外我们还需要注意对用到的jar进行引用,如果报 java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonGenerationException

错误应该是缺少jackson相关的jar

0 0
原创粉丝点击