SpringMvc receives list and array parameters (@RequestParam, @PathVariable)

foreword

A while ago, I saw that my friend used strings to receive parameters, and then separated them into arrays with commas and then converted them into LIst:

@DeleteMapping("/{ids}")
public String delete(@PathVariable String ids) {
    
    
	List<String> idLis = Arrays.asList(ids.split(","));
	....
}

The above conversion codes exist in large quantities in the project, which is not very elegant.
Therefore, it is recommended to use the Array and List parameter conversion mechanism that comes with Spring MVC directly as follows.

Receive List parameter

The sample code is as follows:

Note: For the specific request format, see the notes on the Controller method

@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;

}

Receive Array parameter

The sample code is as follows:

Note: For the specific request format, see the notes on the Controller method

@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;

}


Reference:
https://www.baeldung.com/postman-form-data-raw-x-www-form-urlencoded
@RequestParam and @PathVariable pass array, experiment of json array type parameter

おすすめ

転載: blog.csdn.net/luo15242208310/article/details/129852525