1

Spring Boot- Spring MVC Project below Rest API

@GetMapping(value = "bulk/{speakerIds}")
@ResponseStatus(HttpStatus.MULTI_STATUS)
@Operation(summary = "Gets Speakers to a max Limit of 10")
public Map<Long, Object> getMasterClassBulk(@PathVariable List<Long> speakerIds) {

    return speakerHandler.getSpeakers(speakerIds);

}

Is there a way to add List Max Size validation ? Eg: When the input list exceeds size 10, throw 404-Bad Request

Tried below code, but it does not work.

@PathVariable @Size(max=10) List<Long> speakerIds
3
  • 1
    try annotating class with @Validated annotation Commented May 2, 2021 at 15:54
  • without validation it works??.how:: /bulk/[1,2,3] ?? (urlencoded: /bulk/%5B1%2C2%2C3%5D ;) Commented May 2, 2021 at 16:27
  • The count of path variables cannot be variable!(...) ...use parameters/body instead! ;) Commented May 2, 2021 at 16:35

2 Answers 2

1

Try anotating the class with @Validated and try this on method argument:

@Valid @PathVariable @Size(max=10) List<Long> speakerIds
Sign up to request clarification or add additional context in comments.

Comments

0

Is there a way to add List Max Size validation ? Eg: When the input list exceeds size 10, throw 404-Bad Request

If this is your question than you can simplify it by using

public Map<Long, Object> getMasterClassBulk(@PathVariable List<Long> speakerIds) {
    
    if(speakerIds.size()>10)
      throw new BadRequestException("Length too long");

    return speakerHandler.getSpeakers(speakerIds);

}

Create BadRequestException class like this:

@ResponseStatus(HttpStatus.BAD_REQUEST)  
public class BadRequestException extends RuntimeException   
{  
public BadRequestException(String message)   
{  
super(message);  
}  
}  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.