@NotNull @NotBlank @Valid在 Spring 引导休息 API 验证中不起作用



@NotNull、@NotEmpty@NotBlank注释在我的 rest 控制器中不起作用。我的要求是限制控制器的流量,并在没有必需参数的情况下击中控制器时出现 400 错误。但是当我将空或空标头传递给我的控制器时,我没有收到 400 错误。我的控制器命中我的处理程序类,这不是预期的行为

下面是我的控制器

@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());
}
}
}

您需要通过向控制器添加@Validated注释来启用请求参数和路径变量的验证,以便执行验证。

使用这个 maven 依赖项以使这些工作

<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.**

最新更新