【问题标题】:@NotNull @NotBlank @Valid is not working in spring boot rest api validation@NotNull @NotBlank @Valid 在spring boot rest api验证中不起作用
【发布时间】: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


【解决方案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.

【讨论】:

  • 这是我的问题的最终解决方案。
【解决方案2】:

使用这个 maven 依赖项来完成这些工作

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    <version>2.3.2.RELEASE</version>
</dependency> 
                                                                    

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 2018-06-23
      • 2019-04-08
      • 2021-06-04
      • 2022-01-17
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多