问题描述
我对 Spring Boot 和依赖 jakarta-validation-api 有疑问.
实际上我有一个简单的 DTO,它包含一些属性.但是当我在 @Valid 注释中调用 REST 函数时,不会验证这些属性.
有人能找到我的错误吗?
我的 pom.mxml 依赖项的片段:
<groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.0-M1</version><relativePath/><!-- 从存储库中查找父级 --></父母><依赖><groupId>jakarta.validation</groupId><artifactId>jakarta.validation-api</artifactId><version>3.0.0</version></依赖><依赖><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></依赖><依赖><groupId>jakarta.ws.rs</groupId><artifactId>jakarta.ws.rs-api</artifactId><version>3.0.0</version></依赖>
我的 DTO 课程:
导入 jakarta.validation.Valid;@数据公共类 TestDTO 实现了 Serializable {private static final long serialVersionUID = -1362258531757232654L;@NotEmpty(message = "Id 不能为空或为空.")@Size(min = 36, max = 36, message = "Id 必须完全包含 36 个字符.")私人字符串ID;@Min(value = 1, message = "页面大小不能为空或还有一个 REST-Resource 类的片段,其中 DTO 用于主体:
@PostMapping(path = "/")公共整数测试验证(@Valid @RequestBody TestDTO 正文){LOGGER.info(body);返回 1;}
实际上我认为当我调用 Post-REST 方法时,它会在进入方法体之前进行验证,但实际上它进入方法体之前没有经过验证.
是因为雅加达"吗?依赖而不是javax"?
希望你能帮助我:)
就我而言(spring boot 2.4.*).我删除了 jakarta.validation-api
依赖项,然后它就可以工作了.
使用 javax.*
不是 jakarta.*
i have a question to Spring boot and the dependency jakarta-validation-api.
Actually i have a simple DTO which holds some properties. But this properties are not being validated when I call the REST-function within the @Valid annotation.
Can someone find my error?
A snippet of my pom.mxml dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0-M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.0.0</version>
</dependency>
My DTO class:
import jakarta.validation.Valid;
@Data
public class TestDTO implements Serializable {
private static final long serialVersionUID = -1362258531757232654L;
@NotEmpty(message = "Id could not be empty or null.")
@Size(min = 36, max = 36, message = "Id must contains exactly out of 36 characters.")
private String id;
@Min(value = 1, message = "Page size cannot be null or <= 0.")
private Integer page;
}
And also a snippet of the REST-Resource class where the DTO is been used in the body:
@PostMapping(path = "/")
public Integer testValidation(@Valid @RequestBody TestDTO body) {
LOGGER.info(body);
return 1;
}
Actually i would think that when I call the Post-REST method that it will be validated before it will go into the method body, but actually it goes into the method body without has been validated before.
Is it due to "jakarta" dependency instead of "javax"?
Hope you can help me :)
In my case(spring boot 2.4.*).I remove jakarta.validation-api
dependencies,then it works.
use javax.*
not jakarta.*
这篇关于带有 Jakarta Validation Api 的 SpringBoot 未使用 @Valid Annotation 进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!