Spring Boot - 从 2.2.5 升级到 2.3.0 后验证停止工作

mar*_*uga 51 java validation spring spring-boot

我已经将 Spring Boot 项目从 2.2.5 迁移到 2.3.0,之后,验证停止工作(根本没有调用它们)。

我在更改日志文档(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>
Run Code Online (Sandbox Code Playgroud)

我的 pom 父母是:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath></relativePath>
</parent>
Run Code Online (Sandbox Code Playgroud)

我的控制器看起来像这样:

@PostMapping( value = "/signup", consumes = MediaType.APPLICATION_JSON_VALUE )
@ResponseStatus( value = HttpStatus.OK )
public void signUp( @Valid @RequestBody ClientDto clientDto )
{
    onboardingService.signUp( clientDto );
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我找到了问题所在,请查看下面的答案!

感谢大家的帮助!

Bra*_*lva 89

验证启动器不再包含在 Web 启动器中。

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>
Run Code Online (Sandbox Code Playgroud)


Lal*_*K90 29

根据 spring boot 2.3.1 版本不再包含 spring-boot-starter-validation with spring starter

如何添加启动器验证

行家

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

摇篮

dependencies {
  ...
  implementation 'org.springframework.boot:spring-boot-starter-validation'
}
Run Code Online (Sandbox Code Playgroud)

裁判发布说明

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters


Est*_*her 7

如果您遇到例如:无法看到返回给客户端的验证错误(默认消息)的问题,您可以执行以下操作:

最佳解决方案 1: 只需添加 devtools。这应该可以解决问题。在我这样做之后,我所有的绑定结果都返回给了客户端。我建议你先测试一下:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

解决方案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>
Run Code Online (Sandbox Code Playgroud)

现在有必要将java/resources/application.properties中的“包含绑定错误”设置为“始终”。“消息”也是如此,尽管我认为这是可选的。

server.error.include-message=always
server.error.include-binding-errors=always
Run Code Online (Sandbox Code Playgroud)

解决方案 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>
Run Code Online (Sandbox Code Playgroud)

通过回滚到旧版本,这对我来说非常有效。虽然 id 喜欢在某一天更新我的 Spring Boot 版本。(重温解决方案 1 和 2)