我已经将Spring Boot项目从2.2.5迁移到2.3.0,此后,验证停止工作(根本没有调用)。
我在changelog文档(https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3.0-M1-Release-Notes)中读到,spring-boot-starter-validation
现在需要作为依赖项手动添加。
因此,我将其添加到了pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
我的pom父母是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath></relativePath>
</parent>
我的控制器如下:
@PostMapping( value = "/signup", consumes = MediaType.APPLICATION_JSON_VALUE )
@ResponseStatus( value = HttpStatus.OK )
public void signUp( @Valid @RequestBody ClientDto clientDto )
{
onboardingService.signUp( clientDto );
}
编辑:
我有能力找到问题,请检查下面的答案!
感谢大家的帮助!
答案 0 :(得分:48)
验证启动器不再包含在网络启动器中。
spring-boot-starter-validation不再是spring-boot-starter-web和spring-boot-starter-webflux的可传递依赖项。
添加此依赖项以进行验证。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
答案 1 :(得分:23)
根据Spring Boot 2.3.1发布 不再包含带有Spring Starter的spring-boot-starter-validation
如何在
上添加入门验证专家
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
成绩
dependencies {
...
implementation 'org.springframework.boot:spring-boot-starter-validation'
}
裁判发布说明
答案 2 :(得分:2)
实际上,单元测试中有错误。验证效果很好。
对于那些来这里遇到同样问题的人,很可能您缺少像上面的Braian Silva建议的那样将以下依赖项添加到pom.xml中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
感谢您的帮助!
答案 3 :(得分:1)
例如,如果遇到以下问题:无法看到返回给客户端的验证错误(默认消息),则可以执行以下操作:
最佳解决方案1: 只需添加devtools。这应该可以解决问题。完成此操作后,我所有的绑定结果都返回给客户端。我建议您先进行测试:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
解决方案2:
我发现这是由于使用Spring Boot 2.3+ 因此,如果您使用的是Spring Boot 2.3或更高版本,请将此依赖项添加到pom.xml文件中,因为它不再包含在“ web”依赖项本身中。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
现在有必要将 java / resources / application.properties 中的“包含绑定错误”设置为“始终”。 “消息”也一样,尽管我认为这是可选的。
server.error.include-message=always
server.error.include-binding-errors=always
解决方案3:(在我发现解决方案2可能同样有用之前)
所以我发现这是由于安装了Spring boot 2.3+。但是我在Spring Boot v2.3 +中找不到有关@Valid的新更新用法的警告消息。
因此,我最终通过调整pom.xml文件中的发行版来切换回Spring boot v2.2.10(2.2的最新版本):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
通过回滚到较早的版本,这对我来说非常理想。虽然id喜欢有一天要更新我的Spring Boot版本。 (重新访问解决方案1和2)
答案 4 :(得分:0)
我在这里也遇到类似的问题。但就我而言,验证警告我在oracle模式中遗漏了一个序列,但是序列在那里。我认为这是一个错误。.我将继续使用2.4.0版本。
答案 5 :(得分:-1)
嗨,您必须使用@Validated
注释来注释您的控制器类,请参见以下示例:
出于测试目的,请尝试注释@Validated
注释,您不会注意到javax.validation.ConstraintViolationException: hello.name: size must be between 4 and 10
,但是一旦将其放回原位即可。
更多技术信息,请点击Difference between @Valid and @Validated in Spring
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Validated
@RestController
@RequestMapping("/hello")
class HelloController {
@GetMapping
public String hello(@Valid
@NotNull(message = "Name cannot be empty")
@Size(min = 4, max = 10) @RequestParam("name") String name) {
return "Hello, " + name + "!";
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 6 :(得分:-1)
上述问题的简单解决方案是添加:
<repositories>
<repository>
<id>Central Maven repository</id>
<name>Central Maven repository https</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
到您的POM.xml文件。 这将解决此版本的问题
Vipul。