在Spring Boot REST API验证中,@NotNull @NotBlank @Valid无法正常工作。

3

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

请检查以下链接是否有帮助:https://dev59.com/x2sz5IYBdhLWcg3w9ssQ。很可能是因为未正确引用 hibernate-validator.jar 文件。 - YoManTaMero
spring-boot-starter-validation作为依赖项添加。因为自Spring Boot 2.3.0起不再包含它(或者您手动包含依赖项而不是启动器并且没有实现)。 - M. Deinum
3个回答

8

为了使验证生效,您需要通过向控制器添加@Validated注释来对请求参数和路径变量进行验证。


3
使用这个Maven依赖项,以使它们起作用。
<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.**

这是我问题的最终解决方案。 - Udara Seneviratne
需要在控制器中添加@Validated注释,以便对请求参数和路径变量进行验证。这样才能执行验证。 - testing_22
是的,已经突出显示了相同之处。 - Sandeep Jain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接