传值

POST

JSON

  1. @PostMapping
  2. public JsonResult<?> post(@Validated @RequestBody ActivityCreateDTO activityCreateDTO) {}
@ResponseBody
@RequestMapping(value = "/addItemCategories", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public JsonData addItemCategories(HttpServletRequest request,@RequestBody Map<String, Object> json) throws InterruptedException {}

x-www-form-urlencoded

@PostMapping("/test")
public void test(Test test) {
    System.out.println("msg:"+test.getMsg());
}
@PostMapping("/test")
public void test(Integer status,String msg,Integer data) {
    System.out.println("msg:"+msg);
}

PUT

@PutMapping("/{actId}/abolish")
public JsonResult<?> abolishActivity(@PathVariable("actId") Integer actId) {}

DELETE

@DeleteMapping("/{actId}")
public JsonResult<?> deleteActivity(@PathVariable("actId") Integer actId) {}

GET

当然,你也可以这么实现, @RequestParam(value=”username”, required=true) , required 默认为 true,如果前台不传递此参数,后台会报错。如果设置为 false,如果不传,默认为 null。

@GetMapping
public JsonResult<?> getActivityList(Integer status,
String keyword,
@Range(min = 1, message = "页码最小为1") @RequestParam(defaultValue = "1") int page,
@Range(min = 1, max = 100, message = "单页数量最大为1-100") @RequestParam(defaultValue = "10") int count) {

}
@GetMapping("/{actId}/awards_users")
public JsonResult<?> awardsUsers(@PathVariable("actId") Integer actId) {
return JsonResult.ok(toCActivityService.getAwardsUsersList(actId, 50));
}

@GetMapping("/{actId}/intro")
public JsonResult<?> intro(@PathVariable("actId") Integer actId) {
return JsonResult.ok(toCActivityService.getActivityIntro(actId));
}
@GetMapping("/{actId}")
public JsonResult<?> activityDetail(@PathVariable("actId") Integer actId) {}

多参数无实体 map

json

@ResponseBody
@RequestMapping(value = "/getItems", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public Object getItems(HttpServletRequest request, @RequestBody Map<String, Object> json) {}

对象
image.png

数组

image.pngimage.png

集合实体对象

image.png

  • @RequestBody注解,必须与contentType 类型application/json配合使用。
  • @RequestParam注解,必须与contentTyp类型application/x-www-form-urlencoded配合使用,其为默认类型。
  • JSON.stringify()把对象类型转换为字符串类型,配合@RequestBody注解和contentType 类型application/json使用。

    @ResponseBody
    @RequestMapping(value = "/uploadItems", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JsonData uploadItems(HttpServletRequest request,
    @RequestBody String dataList) throws InterruptedException {
    String accessToken = request.getAttribute("access_token").toString();
    Long shopId = Long.valueOf(request.getAttribute("shop_id").toString());
    
    JSONObject j1 = JSONObject.parseObject(dataList);
    List<OnlineUpdateItemLogs> onlineUpdateItemLogsList = JSONObject.parseArray(j1.getJSONArray("dataList").toJSONString(), OnlineUpdateItemLogs.class);
    
    onlineDto.uploadCategories(accessToken, shopId, 1,onlineUpdateItemLogsList);
    
    return JsonData.buildSuccess();
    }
    

传递集合实体对象一对多
image.pngimage.png

文件上传

@ResponseBody
@RequestMapping(value="/importItems", method = RequestMethod.POST)
public JsonData importItems(@RequestParam Map<String,String> map,@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {}

校验

注解列表
https://blog.csdn.net/u012882327/article/details/86609974

使用
https://blog.csdn.net/justry_deng/article/details/86571671?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-4.channel_param

Snipaste_2020-10-05_12-31-36.jpgSnipaste_2020-10-05_12-35-07.jpgSnipaste_2020-10-05_12-37-12.jpgSnipaste_2020-10-05_12-37-45.jpgSnipaste_2020-10-05_12-38-12.jpg