Jonathan Hagen
Jonathan Hagen

Reputation: 742

How to validate parameters for rest controller

I have multiple parameters that are not required. I was wondering if it is possible to validate non-required parameters. For instance, I have "name" field which is not a required parameter. I want to check if it is not blank when api is exposed.

@GetMapping
public Response method(@RequestParam("firstname") @NotBlank String firstName) {
...
...

I am new to spring and rest controller so I am not sure if this is even right approach. I want this api to not require firstname but if it is in parameter, I want to make sure something is in there. Is this possible to achieve? If so, how?

Upvotes: 0

Views: 354

Answers (1)

Eklavya
Eklavya

Reputation: 18480

Use @Size(min = 1) instead of @NotBlank then null elements are considered valid.

@GetMapping
public Response method(@RequestParam("firstname") @Size(min = 1) String firstName) {

And make sure you are using proper configuration to validate request param

Upvotes: 1

Related Questions