springmvc ajax 获取json 报错 406 的解决方案
来源:互联网 发布:网络分销渠道有哪些 编辑:程序博客网 时间:2024/06/07 07:31
使用的spring 版本都在4.3.5
使用的jackson版本2.8.5
----------------------------------------------------------------------------------------------------------------------------------------------------
使用的jackson maven 配置:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>${jackson.version}</version>
</dependency>
servlet配置:
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
<bean
class="org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter">
<property name="objectMapper" ref="xmlMapper" />
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 有时我们返回的字段存在null的情况我们需要对其进行处理,objectMapper提供了默认的几种处理方式配置如下 -->
<bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
</bean>
<bean id="xmlMapper" parent="objectMapper" p:createXmlMapper="true" />
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
遇到的问题
我使用ajax请求后台controller,
$.ajax({
url:'/test-app/test/info.htm',
data : {
},
type:'post',
dataType:'json',
success:function(data){
alert(data)
}
});
这个时候报错406
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
解决方案:
因为我在web.xml里面配置的spring 的servlet 拦截后缀名.htm结尾的请求,这样的话新版本的jackson默认是认为要返回text/html的格式,所以报错406了,最后修改为
<!-- spring mvc servlet -->
<servlet>
<description>spring mvc servlet</description>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
这样同时拦截.htm和.json结尾的请求,然后ajax改为
$.ajax({
url:'/test-app/test/info.json',
data : {
},
type:'post',
dataType:'json',
success:function(data){
alert(data)
}
});
这样就可以正常返回json数据了。
- springmvc ajax 获取json 报错 406 的解决方案
- springmvc响应json数据的时候报错406解决方案
- SpringMVC通过Ajax获取JSON数据报406错误
- SpringMVC获取AJAX数据报406错误
- SpringMVC ajax传递json数组到后台报406错误的解决办法
- $.ajax json数据到server,报400错误的解决方案
- ajax无法返回json数据报错406的解决方法
- ajax跨域获取数据解决方案---JSON
- SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案
- json格式,ajax报错
- HttpClient获取JSON报错406问题解决
- springMVC浏览器接受json报406错误的解决方法
- springMVC浏览器接受json报406错误的解决方法
- springMVC浏览器接受json报406错误的解决方法
- springMVC浏览器接受json报406错误的解决方法
- ajax在IE下json下载避免解决方案 springmvc
- 关于springmvc注解扫描报错500的解决方案
- 关于springmvc注解扫描报错500的解决方案
- XML 文件详解【命名空间】
- 自动化测试工具TestWriter免费版发布最新Beta版本
- SQLServer DROP 和 TRUNCATE Table 时数据文件无变化
- aqtest 004
- Load average
- springmvc ajax 获取json 报错 406 的解决方案
- sql查询表默认约束 删除和添加约束
- 二级C 2017.6.14
- lodop打印维护功能
- 工作中遇到一些知识点2017.6.14
- eclipse中复制导入的项目并且修改了项目名字,项目后面的括号显示原来项目的名字
- VS2010+QT5开发所踩的坑以各种问题解决
- POJ 1849 Two
- java线程安全之——可重入锁