postman 测试 Array、List、Map 入参 API 正确姿势

目录

一 Array 

1.1 spring 以 @RequestParam 解析简单数组

1.2 spring 以 @RequestBody 解析简单数组

二 List

2.1 spring 以 @RequestParam 解析简单列表

2.2 spring 以 @RequestBody 解析简单列表

2.3 spring 以 @RequestBody 解析列表对象

三 Map

3.1 spring 以 @RequestParam 解析map 

3.2 spring 以 @RequestBody 解析map 

扫描二维码关注公众号,回复: 12236707 查看本文章

一 Array 

1.1 spring 以 @RequestParam 解析简单数组

@RequestMapping(value = "/getByIds" ,method = RequestMethod.POST)
public RetDTO getByIds(@RequestParam(value = "ids") Long[] ids){
    return RetDTO.getReturnJson(userService.getByIds(ids));
}

postman 请求方式一

postman 请求方式二

postman 请求方式三

postman 请求方式四

1.2 spring 以 @RequestBody 解析简单数组

@RequestMapping(value = "/getByIds" ,method = RequestMethod.POST)
public RetDTO getByIds(@RequestBody Long[] ids){
    return RetDTO.getReturnJson(userService.getByIds(ids));
}

postman 请求方式

二 List

2.1 spring 以 @RequestParam 解析简单列表

@RequestMapping(value = "/getByIds" ,method = RequestMethod.POST)
public RetDTO getByIds(@RequestParam("ids") List<Long> ids){
    return RetDTO.getReturnJson(userService.getByIds(ids));
}

postman 请求方式一

postman 请求方式二

2.2 spring 以 @RequestBody 解析简单列表

@RequestMapping(value = "/getByIds" ,method = RequestMethod.POST)
public RetDTO getByIds(@RequestBody List<Long> ids){
    return RetDTO.getReturnJson(userService.getByIds(ids));
}

postman 请求方式

2.3 spring 以 @RequestBody 解析列表对象

@RequestMapping(value = "/getByIds" ,method = RequestMethod.POST)
public RetDTO getByIds(@RequestBody List<User> userList){
    return RetDTO.getReturnJson(userService.getByIds(userList));
}

postman 请求方式

三 Map

3.1 spring 以 @RequestParam 解析map 

@RequestMapping(value="/getById", method=RequestMethod.GET)
public RetDTO<User> getById(@RequestParam Map<String, Long> map) {
    return RetDTO.getReturnJson(userService.getUserById(map.get("id")));
}

postman 请求方式

3.2 spring 以 @RequestBody 解析map 

@RequestMapping(value="/getById", method=RequestMethod.GET)
public RetDTO<User> getById(@RequestBody Map<String, String> map) {
    return RetDTO.getReturnJson(userService.getUserById(map));
}

微信公众号:「新猿一马」,微信扫一扫。

猜你喜欢

转载自blog.csdn.net/jack1liu/article/details/110905222