spring 报错,Could not find acceptable representation
程序员文章站
2022-05-21 09:29:17
...
使用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
推荐阅读
-
swagger 集成后发布到服务器报错[Could not find file 'D:\\home\\site\\wwwroot\\bin\\WebAPI.XML]
-
AndroidStudio 报错:Could not find com.android.tools.build:gradle
-
【js】react-native Could not find iPhone 6 simulator 和 Entry, ":CFBundleIdentifier", Does Not Exist 两种报错解决办法
-
MyBatis报错笔记——Could not find parameter map XXX.XXX.xml
-
Could not find artifact org.springframework.cloud:spring-cloud-netflix-eureka-server报错
-
淘淘商城第60讲——搭建Solr集群时,报错:org.apache.solr.common.SolrException: Could not find collection : core1
-
flutter项目报错:Could not find method flutter() for arguments
-
swagger 集成后发布到服务器报错[Could not find file 'D:\\home\\site\\wwwroot\\bin\\WebAPI.XML]
-
SpringBoot报406,web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
-
spring 报错,Could not find acceptable representation