SpringMVC出错日记——前端AJAX请求发送JSON数据,出现406错误

1、SpringMVC处理JSON数据要依赖3个jar包

<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.9</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.9</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9.3</version>
</dependency>

2、SpringMVC配置文件 dispatcher-servlet.xml 中要开启一个注解

<mvc:annotation-driven></mvc:annotation-driven>

3、对应的controller层里面方法要加@ResponseBody注解

@Controller
@RequestMapping("springmvc")
public class hello {
    //    测试json
    @ResponseBody       //把返回的数据变为了json对象 //    告诉springmvc此时的返回值不是一个view页面,而是一个ajax 请求的返回值数据
    @RequestMapping(value="testJson")
    public List<Student> testJson(){
        Student s1 = new Student(1,23,"zs");
        Student s2 = new Student(2,24,"ls");
        Student s3 = new Student(3,25,"ww");
        System.out.println("-----------------------------");
        List<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        return students;
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值