-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatorBean
154 lines (117 loc) · 4.14 KB
/
ValidatorBean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
spring.jpa.properties.javax.persistence.validation.mode=auto
# Enable debug logging for validation
logging.level.org.springframework.validation=DEBUG
logging.level.org.hibernate.validator=DEBUG
logging.level.your.package.name=DEBUG
import javax.validation.constraints.NotBlank;
public class User {
@NotBlank(message = "Username cannot be blank")
private String username;
// Getters and Setters
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import javax.validation.Validator;
@Configuration
public class ValidatorConfig {
@Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
}
import org.springframework.stereotype.Service;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;
@Service
public class StudentService {
private final Validator validator;
// Injecting the Validator bean
public StudentService(Validator validator) {
this.validator = validator;
}
public void createStudent(Student student) {
// Validate the student object
Set<ConstraintViolation<Student>> violations = validator.validate(student);
if (!violations.isEmpty()) {
StringBuilder errors = new StringBuilder("Validation failed: ");
for (ConstraintViolation<Student> violation : violations) {
errors.append(violation.getPropertyPath()).append(" ").append(violation.getMessage()).append("; ");
}
throw new IllegalArgumentException(errors.toString());
}
// Proceed with saving the student if validation passes
// entityManager.persist(student); // Your persistence logic here
}
}
----
@Configuration
public class ValidationConfig {
@Bean
public Validator validator() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
return factory.getValidator();
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
}
@Service
@Validated
public class StudentService {
private final Validator validator;
private final EntityManager entityManager;
@Autowired
public StudentService(Validator validator, EntityManager entityManager) {
this.validator = validator;
this.entityManager = entityManager;
}
@Transactional
public void saveStudent(@Valid Student student) {
Set<ConstraintViolation<Student>> violations = validator.validate(student);
if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations);
}
entityManager.persist(student);
}
}
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>8.0.1.Final</version>
</dependency>
import org.hibernate.validator.HibernateValidator;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
@Configuration
public class ValidationConfig {
@Bean
public ValidatorFactory validatorFactory() {
return Validation.byProvider(HibernateValidator.class)
.configure()
.buildValidatorFactory();
}
@Bean
public Validator validator(ValidatorFactory validatorFactory) {
return validatorFactory.getValidator();
}
@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setProviderClass(HibernateValidator.class);
return bean;
}
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor(Validator validator) {
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
processor.setValidator(validator);
return processor;
}
}