0

I have the GET request like

/api/v1/data?name=aaa&name=bbb&name=cccc&name=dd&name....

I want to limit the number of 'name' param, it must be no more than 100 (configurable in properties file)

Here is my controller

public Data getDataByNames(@RequestParam(value = "name") List<String> names) {
    return userService.getDataByNames(names);
}

How can I do that? Thanks

Update: yeah, I can check in service layer: if(names.size() < 100) {...} but it seem not professional

Loc Le
  • 537
  • 1
  • 8
  • 21
  • Possible duplicate of [Spring Web MVC - validate individual request params](https://stackoverflow.com/questions/6203740/spring-web-mvc-validate-individual-request-params) – Ori Marko Dec 05 '18 at 06:27
  • @user7294900 no, It's not the same, did you read the contents, your suggestions is validate for one request param 'foo_name', my question is for validate number of the same request param. – Loc Le Dec 05 '18 at 06:32

1 Answers1

0

If you don't need to max number of elements to be configurable, you can use annotation @Size. Step 1. Define a wrapper for your request param

public class NameWrapper {

    @Size(max = 100)
    @NotEmpty
    private List<String> name;
    //getters and setters
}

Step 2. Add the wrapper as a parameter to your controller method

public Data getDataByNames(@Valid NameWrapper nameWrapper) {

If you want to be from configurable from application properties, you should define your own custom argument resolver.

Adina Rolea
  • 2,031
  • 1
  • 7
  • 16