后端接口接收数据的方式
1.Query String(查询字符串方式,url后通过?引出的参数) 适用于任何请求(?号拼接),后端直接用参数接受即可
//购买单个商品
@PutMapping("/buyProduct")
public Result buyProduct(@RequestHeader(Constants.HEADER_NAME_TOKEN) String token,Integer p_id){
TokenUser tokenUser = TokenUtils.getTokenUser(token);
Integer u_id = Integer.parseInt(tokenUser.getUserId());
service.buyProduct(u_id,p_id);
return Result.OK();
}
2.请求体的方式(payload方式json请求体)适用post、delete、put等请求,后端要用对象或者数组接收
//添加商品
@PostMapping("/insertProduct")
public Result insertProduct(@RequestBody Product product, @RequestHeader(Constants.HEADER_NAME_TOKEN) String token){
TokenUser tokenUser = TokenUtils.getTokenUser(token);
Integer u_id = Integer.parseInt(tokenUser.getUserId());
product.setU_id(u_id);
service.insertProduct(product);
return Result.OK();
}
3.路径参数 (localhost:8081/role/{a}),后端直接用参数接收
@PutMapping("/ZuoFei/{o_id}")
public Result ZuoFei(@PathVariable Integer o_id){
service.ZuoFei(o_id);
return Result.OK();
}
接收数据到的注解
-
@RequestHeader(token) String token 从请求头中拿到名字为token的数据
-
@RequestBody Product product 获取json对象,并转化为java对象
-
@PathVariable 获取路径参数