get 请求参数是对象怎么办

在开发中,我们知道 Restful 对查询的规范是使用 get 请求,而一个管理画面的查询条件是多个,那么就需要前台传给后台一个对象。

本篇文章介绍如何使用 get 请求传递对象。

前端传参

在前台不能使用  this.$axios.get  ,而是需要使用  this.$axios.request ,使用方法如下。

this.$axios.request({
    method: 'GET',
    url: '/person/getByConditions',
    params: {
        name: 'Mary',
        sex: '女',
    }
}).then((res) => {
    this.records = res
})

params 也可以直接指定一个对象,和上面的写法作用一样。

const reqData = {
    name: 'Mary',
    sex: '女'
}
this.$axios.request({
    method: 'GET',
    url: '/person/getByConditions',
    params: reqData
}).then((res) => {
    this.records = res
})

后台接收

存放查询条件的 Dto

/**
 * person 表查询用dto
 * @author admin
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PersonQueryDto {

    private String name;

    private String sex;

}

Controller 中的 get 请求对于参数不需要添加任何注解。

@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    PersonService personService;

    @GetMapping("/getByConditions")
    public List<Person> getByNameAndSex(PersonQueryDto personQueryDto) {
        return personService.getByNameAndSex(personQueryDto);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值