【发布时间】:2020-10-04 12:38:17
【问题描述】:
@NotNull、@NotEmpty、@NotBlank 注释在我的休息控制器中不起作用。我的要求是限制控制器的流量,当我在没有所需参数的情况下点击控制器时出现 400 错误。但是当我将 null 或空标题传递给我的控制器时,我没有收到 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());
}
}
}
【问题讨论】:
-
检查这是否有帮助:stackoverflow.com/questions/7545231/…。很有可能没有正确引用 hibernate-validator.jar。
-
将
spring-boot-starter-validation添加为依赖项。因为从 Spring Boot 2.3.0 开始不再包含它(或者您手动包含依赖项而不是启动器并且没有实现)。
标签: spring spring-boot rest spring-mvc