Spring @RequestHeader validate for blank string

826 Views Asked by At

I have a spring service that receives a header parameter like this:

@RequestHeader(name = "token", required = true) String token

but it only validates if the header is present and not if it is blank or just spaces,

if I add @NotBlank or @Valid makes no change,

How can I annotate a header parameter so it is validated for blank strings?

thnx in advance,

1

There are 1 best solutions below

1
On

Validate Request Header docs

The spring boot allows to validate a request header as like other request parameters. The annotation @Validated is used to validate the request header. And add @NotBlank for validation

import org.springframework.validation.annotation.Validated;


@RequestHeader(name = "token", required = true) @NotBlank String token