1、前言
在ssm框架中向前台返回 json 数据很常见,而今天遇到了一个目前所学阶段,没有遇到过的问题,即后台向前台返回 json数据,前台显示 406。
这里是后端从数据库里读取数据正常的,在controller里可以输出数据。
2、出现原因
当我们查看响应头时,会发现,Content-Type: text/html;charset=utf-8,它为 text/html的类型,不是 json的类型,即数据没有转为 json发送。

3、解决方法
通过上述的原因分析,可知,需要将数据转为 json 来发送。
1、所以这里直接在 pom文件中加入依赖:
点击查看代码
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.1</version>
</dependency>
2、还需再配置开启注解驱动:
<mvc:annotation-driven/>
为什么还要配置注解驱动,直接添加依赖不行吗?答案是不行的。
具体如下:
为什么配置注解驱动
因为当 springmvc 配置了注解驱动后,如果classpath 里面包含jackson 包,则自动注册 MappingJackson2HttpMessageConverter,从而支持json 输出。
浙公网安备 33010602011771号