3

@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());
        }
    }
}
durgaPrasad
  • 109
  • 1
  • 3
  • 12
  • Check if this may help: https://stackoverflow.com/questions/7545231/bean-validation-notnull-notblank-and-notempty-does-not-work-in-jsftomcat. Chances are hibernate-validator.jar not referenced correctly. – YoManTaMero Jun 15 '20 at 06:33
  • Add `spring-boot-starter-validation` as a dependency. As that isn't included anymore as of Spring Boot 2.3.0 (or you are manually including dependencies instead of starters and don't have an implementation). – M. Deinum Jun 15 '20 at 06:37

3 Answers3

8

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.

feanor07
  • 3,328
  • 15
  • 27
3

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> 
                                                                    
1
 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.**
Sandeep Jain
  • 1,019
  • 9
  • 13