java 验证不适用于Sping Boot + RestController

42fyovps  于 2023-04-10  发布在  Java
关注(0)|答案(2)|浏览(123)

我有一个带Rest Controller的Sping Boot 项目,需要验证输入数据。以下是我的POM:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/>
    </parent>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>${javax.validation.version}</version>
        </dependency>

这是我的Controller。我也尝试在整个类上放置Validated annotation,而不是在方法上放置Valid。此外,我还尝试在Service类上放置Valid(ated)annotation:

@Slf4j @RestController
//@Validated placing this annotation here didn't helped either
public class MyController {
    @Autowired
    MyService myService;

    private final Counter requestCounter;

    public MyController(MeterRegistry meterRegistry) {
        this.requestCounter = meterRegistry.counter("my.counter");
    }

    @PostMapping("/somepath")
    public ResponseEntity<String> handle(@RequestBody @Valid InputMessage im) {
        log.debug("Received message {}",im);
        requestCounter.increment();
        try {
            myService.handleDoc(im);
            return new ResponseEntity<>("Done", HttpStatus.CREATED);
        }catch (Exception ex){
            log.error("Error with document with message id {}",im.getId(), ex);
            return new ResponseEntity<>("Failed to process request", HttpStatus.BAD_REQUEST);
        }
    }
}

InputMessage类:

import javax.validation.constraints.*;
@Data
public class InputMessage {
    @Size(min = 1, max = 10)
    public String id;

    @NotBlank @Email
    public String email;

    @NotBlank //the same with @NotNull here
    public Integer status;

    @NotBlank
    public String error;
}

无论我在请求中发送哪些数据-我都会收到201 Created。这在测试中以及通过启动应用程序和使用postman都是一样的。此外,我尝试删除try-catch,只留下计数器增量和handleDoc方法。
我应该做什么来启用验证?

5rgfhyps

5rgfhyps1#

首先,你应该修复你的依赖关系,对于验证来说,这三个就足够了:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

此外,@NotBlank验证器不适用于整数,您应该使用@NotNull

import javax.validation.constraints.*;
@Data
public class InputMessage {
    @Size(min = 1, max = 10)
    public String id;

    @NotBlank @Email
    public String email;

    @NotNull
    public Integer status;

    @NotBlank
    public String error;
}
vd8tlhqk

vd8tlhqk2#

UPD:将导入中的所有包从javax更改为jakarta解决了一个问题...

相关问题