如何解决Spring Boot中的ConstraintViolationException



我在spring-boot中编写了一个自定义验证。自定义验证旨在检查两个字段是否匹配。在我尝试提交表格之前,一切都很好。Boom我遇到了错误:

javax.validation.ConstraintViolationException: Validation failed for classes [com.joker.SampleAuthenticationWebApp.model.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='Passwords do not match!', propertyPath=con_password, rootBeanClass=class com.joker.SampleAuthenticationWebApp.model.User, messageTemplate='Passwords do not match!'}
]

这个问题确实阻碍了我的学习进度。非常感谢您的帮助

附言:我一直在寻找解决方案,但都无济于事

注释:

package com.joker.SampleAuthenticationWebApp.validator;
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Constraint(validatedBy = FieldsValueMatchValidator.class)
public @interface FieldsValueMatch {
String message() default "Fields values don't match!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field();
String fieldMatch();
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@interface List {
FieldsValueMatch[] value();
}
}

验证器:

package com.joker.SampleAuthenticationWebApp.validator;
import com.joker.SampleAuthenticationWebApp.model.User;
import org.springframework.beans.BeanWrapperImpl;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class FieldsValueMatchValidator implements ConstraintValidator<FieldsValueMatch, Object> {
private String field;
private String fieldMatch;
@Override
public void initialize(FieldsValueMatch constraintAnnotation) {
this.field = constraintAnnotation.field();
this.fieldMatch = constraintAnnotation.fieldMatch();
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
try {
final Object fieldValue = new BeanWrapperImpl(value).getPropertyValue(field);
final Object fieldMatchValue = new BeanWrapperImpl(value).getPropertyValue(fieldMatch);
boolean isValid = fieldValue == null && fieldMatchValue == null || fieldValue != null && fieldValue.equals(fieldMatchValue);
if (!isValid) {
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()).addPropertyNode(fieldMatch).addConstraintViolation();
return false;
}
return isValid;
}
catch (final Exception ignore) {
// ignore
}
return true;
}
}

用户模型:

package com.joker.SampleAuthenticationWebApp.model;
import com.joker.SampleAuthenticationWebApp.validator.FieldsValueMatch;
import org.hibernate.validator.constraints.Length;
import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Set;
@Entity
@Table(name = "auth_user")
@FieldsValueMatch(field = "password", fieldMatch = "con_password", message = "Passwords do not match!")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "native")
@Column(name = "auth_user_id", unique = true)
private int id;
@NotNull
@NotEmpty(message = "First name is compulsory")
@Column(name = "firstname")
private String firstname;
@NotNull
@NotEmpty(message = "Last name is compulsory")
@Column(name = "lastname")
private String lastname;
@NotNull
@NotEmpty(message = "Email is compulsory")
@Email
@Column(name = "email")
private String email;
@NotNull
@Column(name = "phone")
private String phone;
@NotNull
@NotEmpty(message = "Password is compulsory")
@Length(min = 5, message = "Password length should be at least 5 characters")
@Column(name = "password")
private String password;
@NotEmpty(message = "Confirm Password field is compulsory")
@Transient
private String con_password;
@NotNull
@Column(name = "enabled")
private int enabled;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "auth_user_role", joinColumns = @JoinColumn(name = "auth_user_id"), inverseJoinColumns = @JoinColumn(name = "auth_role_id"))
private Set<Role> roles;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCon_password() {
return con_password;
}
public void setCon_password(String con_password) {
this.con_password = con_password;
}
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}

我也遇到过这个问题。解决此问题的唯一方法是在您要检查的bean的控制器类上包含@ModelAttribute和@Valid的注释。@FieldsValueMatch(字段="密码",字段匹配="con_password",消息="密码不匹配!"(的条件是否为true,之后,在要检查的bean之后立即包含BindingResult。检查BindigResult是否有Errors((,并返回下一步操作。

EX:

@PostMapping("/signup"(
public String注册(@ModelAttribute(name="user"(@Valid user user,BindingResult result,Model Model(
if(result.hasErrors(((
返回";signup_form";;

希望这是清楚的。

相关内容

  • 没有找到相关文章

最新更新