springboot中的406(Not Acceptable)错误

今天遇到一个很奇怪的问题,我用springboot开发时,用ajax请求服务想返回json数据,页面总是报406错误,这个错误使我郁闷了很久,原来我的后台服务是这么写的

看到没value的后面有个.htm,我猜springboot框架会默认以为要返回html页面,所以一直报406(Not Acceptable)

1.后来我把value里面的.htm去掉了

然后什么问题都没有了。

2.还有另外的一种解决方式是(这个配置是我同事找到的)

这个解决办法是在springmvc.xml文件里增加配置

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

原文地址:https://www.cnblogs.com/wuyouwei/p/5553443.html