SpringMVC ResponseBody

SpringMVC快速返回ajax功能

  1. 返回数据是json就可以;
  2. 页面时$.ajax();

原生javaweb

  1. 导入gson
  2. 返回数据用gson转成json
  3. 返回

SpringMVC

  1. 导包
  2. 写配置

添加jackson依赖

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

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.10.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.10.2</version>
    </dependency>

@ResponseBody简介

@ResponseBody把后台pojo转换为json对象,返回到页面

@Controller
public class AjaxTestController {
    @Autowired
    EmployeeDao employeeDao;
    /*
    * 将返回的数据放在相应体中,如果返回的是对象,自动将对象转为json格式
    * */
    @ResponseBody
    @RequestMapping("/getallajax")
    public Collection<Employee> ajaxGetAll(){
        Collection<Employee> all = employeeDao.getAll();
        return all;
    }
}

在这里插入图片描述

@RequestBody简介

RequestBody将请求体中的数据直接返回,也可将请求体中的数据封装直接封装为对象

需要前端页面请求体中的数据为json字符串。get请求没有请求体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值