前言
前一阵看见小伙伴用字符串接收参数,然后使用逗号分隔成数组后再转成LIst:
@DeleteMapping("/{ids}")
public String delete(@PathVariable String ids) {
List<String> idLis = Arrays.asList(ids.split(","));
....
}
如上转换代码在项目中大量存在,不是很优雅,
故推荐了给小伙伴如下直接使用Spring MVC自带的Array、List参数转换机制。
接收List参数
示例代码如下:
注: 具体请求格式参见Controller方法上的注释
@RestController
@RequestMapping("/api/v1/tests")
@Slf4j
public class RestParamsTestController {
//请求格式1:curl -X GET "http://localhost:8080/api/v1/tests/list?ids=1,2,3"
//请求格式2:curl -X GET "http://localhost:8080/api/v1/tests/list?ids=1&ids=2&ids=3"
@GetMapping("/list")
public String getByList(@RequestParam(required = true) List<String> ids) {
String paramJson = JsonUtils.toJson(ids);
log.info("getByList: {}", paramJson);
return paramJson;
}
//请求格式1:curl -X POST "http://localhost:8080/api/v1/tests/list" -H "Content-Type: application/x-www-form-urlencoded" --data-urlencode "ids=1,2,3"
//请求格式2:curl -X POST "http://localhost:8080/api/v1/tests/list" -H "Content-Type: application/x-www-form-urlencoded" --data-urlencode "ids=1" --data-urlencode "ids=2" --data-urlencode "ids=3"
@PostMapping("/list")
public String postByList(ParamListDto dto) {
String paramJson = JsonUtils.toJson(dto);
log.info("postByList: {}", paramJson);
return paramJson;
}
//请求格式:curl -X DELETE "http://localhost:8080/api/v1/tests/list/1,2,3"
@DeleteMapping("/list/{ids}")
public String deleteByList(@PathVariable(required = true) List<String> ids) {
String paramJson = JsonUtils.toJson(ids);
log.info("deleteByList: {}", paramJson);
return paramJson;
}
}
---
@Data
public class ParamListDto {
private List<String> ids;
}
接收Array参数
示例代码如下:
注: 具体请求格式参见Controller方法上的注释
@RestController
@RequestMapping("/api/v1/tests")
@Slf4j
public class RestParamsTestController {
//请求格式1:curl -X GET "http://localhost:8080/api/v1/tests/array?ids=1,2,3"
//请求格式2:curl -X GET "http://localhost:8080/api/v1/tests/array?ids=1&ids=2&ids=3"
@GetMapping("/array")
public String getByArray(@RequestParam(required = true) String[] ids) {
String paramJson = JsonUtils.toJson(ids);
log.info("getByArray: {}", paramJson);
return paramJson;
}
//请求格式1:curl -X POST "http://localhost:8080/api/v1/tests/array" -H "Content-Type: application/x-www-form-urlencoded" --data-urlencode "ids=1,2,3"
//请求格式2:curl -X POST "http://localhost:8080/api/v1/tests/array" -H "Content-Type: application/x-www-form-urlencoded" --data-urlencode "ids=1" --data-urlencode "ids=2" --data-urlencode "ids=3"
@PostMapping("/array")
public String postByArray(ParamArrayDto dto) {
String paramJson = JsonUtils.toJson(dto);
log.info("postByArray: {}", paramJson);
return paramJson;
}
//请求格式:curl -X DELETE "http://localhost:8080/api/v1/tests/array/1,2,3"
@DeleteMapping("/array/{ids}")
public String deleteByArray(@PathVariable(required = true) String[] ids) {
String paramJson = JsonUtils.toJson(ids);
log.info("deleteByArray: {}", paramJson);
return paramJson;
}
}
---
@Data
public class ParamArrayDto {
private String[] ids;
}
参考:
https://www.baeldung.com/postman-form-data-raw-x-www-form-urlencoded
@RequestParam和@PathVariable传递数组,json数组类型参数的实验