@NotNull @NotBlank @Valid is not working in spring boot rest api validation

Viewed 9855

@NotNull, @NotEmpty, @NotBlank annotations are not working in my rest controller. My requirement is to restrict the flow at controller and get 400 error when i hit the controller without required parameters. But when i pass null or empty headers to my controller, i am not getting 400 error. my controller hits my handler class which is not the expected behaviour

Below is my controller

@RestController
@RequestMapping("/intelligent-banking")
public class CrossSellOffersRetrievalController {


    @Autowired
    private CrossSellOffersRetrievalHandler crossSellOffersRetrievalHandler;

    @Autowired
    Environment env;

    @GetMapping(value = "/cross-sell-offers/{interactionPoint}", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<CrossSellOffersRetrievalResponse> getApplicableOffers(
            @RequestHeader(value = "channelId", required = true) @Valid String channelId,
            @RequestHeader(value = "clientId", required = false) String clientId,
            @RequestHeader(value = "actionId", required = true) @NotNull @NotEmpty String actionId,
            @RequestHeader(value = "customerId", required = true) @NotNull @NotBlank String customerId,
            @RequestHeader(value = "cinSuffix", required = true) @NotNull @NotBlank String cinSuffix,
            @RequestHeader(value = "sessionId", required = true) @NotNull @NotBlank String sessionId,
            @RequestHeader(value = "countryCode", required = true) @NotNull @NotBlank String countryCode,
            @PathVariable(value = "interactionPoint", required = true) @NotNull @NotBlank String interactionPoint,
            @RequestParam(value = "numberOfOffers", required = false) Integer numberOfOffers)
            throws CrossSellOffersException {
       try {

                CrossSellOffersRetrievalResponse crossSellOffersResponse = crossSellOffersRetrievalHandler.getCrossSellOffersRetrievalResponse(channelId,
                        customerId, cinSuffix, countryCode, interactionPoint, sessionId, numberOfOffers);
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.set("CustomerId", customerId);
                return new ResponseEntity<>(crossSellOffersResponse, httpHeaders, HttpStatus.OK);
        } 
        catch (Exception e) {
            LOGGER.error("Inside CrossSellOffersRetrievalController::getApplicableOffers::Exception - Exception occurred at getApplicableOffers: {} ",e.getMessage());
            throw new CrossSellOffersException(Constants.ERROR_CODE, e.getMessage());
        }
    }
}
3 Answers

You need to enable validation for both request parameters and path variables via adding @Validated annotation to your controller in order for validations to be executed.

Use this maven dependency inorder make those work

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>2.3.2.RELEASE</version>
</dependency> 
                                                                    
 was using This dependency of validation in spring boot and didn't work ,due to version upgrade of spring boot to 2.4.0

<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>

Replaced it with spring-boot-starter-validation and it worked .

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot- 
starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.4.0</version>
</dependency>

**Need to enable validation for both request parameters and path variables via adding @Validated annotation to your controller in order for validations to be executed.**
Related