HttpMediaTypeNotAcceptableException: Could not find acceptable representation报错解决方法

在Controller中使用@ResponseBody向客户端返回JOSN数据时一直报No converter found for return value of type: class java.util.LinkedHashMap,检查了pom文件,JSON包都有导入:
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.7.4</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
	<version>2.7.4</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-annotations</artifactId>
	<version>2.7.4</version>
</dependency>

spring-mvc.xml中也配置了MessageCoverters:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
	<property name="messageConverters">  
		  <list>  
		       <ref bean="mappingJacksonHttpMessageConverter" />  
		  </list>  
	</property>  
</bean>  
		        
<bean id="mappingJacksonHttpMessageConverter"  
		    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
		     <property name = "supportedMediaTypes">  
				  <list>  
				    <bean class="org.springframework.http.MediaType">  
				     <constructor-arg index="0" value="text"/>  
				     <constructor-arg index="1" value="plain"/>  
				     <constructor-arg index="2" value="UTF-8"/>  
				    </bean>  
				    <bean class="org.springframework.http.MediaType">  
				     <constructor-arg index="0" value="*"/>  
				     <constructor-arg index="1" value="*"/>  
				     <constructor-arg index="2" value="UTF-8"/>  
				    </bean>  
				    <bean class="org.springframework.http.MediaType">  
				     <constructor-arg index="0" value="text"/>  
				     <constructor-arg index="1" value="*"/>  
				     <constructor-arg index="2" value="UTF-8"/>  
				    </bean>  
				     <bean class="org.springframework.http.MediaType">  
				     <constructor-arg index="0" value="application"/>  
				     <constructor-arg index="1" value="json"/>  
				     <constructor-arg index="2" value="UTF-8"/>  
				    </bean>  
				  </list>  
			</property>  
 </bean>   

配置都齐了,还是一直报错,经过几番搜索后发现是跟访问的后缀有关,比如使用*.htm或者*.html,默认就会使用text/html,将其改成其它后缀比如*.shtml后问题得以解决!!!

猜你喜欢

转载自blog.csdn.net/qq_33459156/article/details/80762735