问题源码
@PostMapping("/save")
public Result save(@Validated @RequestBody User user){
return Result.success(user);
}
首先,字段前面要用@Valid而不是@Validated,而且应该在@Valid字段后紧跟一个BindingResult接收返回
因此正确的Controller代码如下
@PostMapping("/save")
public Result save(@RequestBody @Valid User user, BindingResult bindingResult){
if(bindingResult.hasErrors()){
System.out.println(bindingResult.getFieldError().getDefaultMessage());
}
return Result.success(user);
}
其次,在Bean实体类中应该用@NotBlank在String类型上而非@NotNull【基本数据类型】或@NotEmpty【集合】。
而且引用的包应该是
import org.hibernate.validator.constraints.NotBlank;
而非import javax.validation.constraints.NotBlank;
新版SpringBootStarter里没有自动注入这个依赖
org.hibernate.validator hibernate-validator 6.1.7.Final
但是会报错说依赖冲突
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
javax.el.ELManager.getexpressionFactory(ELManager.java:38)
The following method did not exist:
javax.el.ELUtil.getexpressionFactory()Ljavax/el/expressionFactory;
The method's class, javax.el.ELUtil, is available from the following locations:
jar:file:/wlfullclient.jar!/javax/el/ELUtil.class
jar:file:/micoservice/apache-maven-3.6.3/repository/org/glassfish/jakarta.el/3.0.3/jakarta.el-3.0.3.jar!/javax/el/ELUtil.class
The class hierarchy was loaded from the following locations:
javax.el.ELUtil: file:/src/main/resources/lib/wlfullclient.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.el.ELUtil
Process finished with exit code 1
这情况99%是依赖冲突了,去
下面找找所有和el有关的,发现有一个带el且点开后发现包含了javax.el,把这个包
直接Open Library Settings在里面删除即可。【注意,不是图片里这个依赖!自己去找哪个带了javax.el我的当时已经删除了】



