Skills and skills to solve the common problems of Java Ee Validation framework
Skills and skills to solve the common problems of Java Ee Validation framework
introduction:
Java Ee (Enterprise Edition) provides specifications and APIs for building enterprise -level applications.One of the important specifications is Validation, which allows developers to verify the user input and ensure the integrity and accuracy of the data.However, when using the Java Ee value framework, developers may encounter some common problems.This article will introduce some skills and skills to solve these problems, and provide some Java code examples to help developers better understand and apply these technologies.
1. Question: How to customize verification messages?
solution:
The Java Ee Validation framework uses the annotation to declare the verification rules and provides the default error message.However, in some cases, developers may need to customize verification messages to meet their needs.You can create a custom verification message by creating a validationalMessages.properties file and placing it on the root directory of the class path.In this file, the custom error message of each verification rule can be specified.The example code is as follows:
#ValidationMessages.properties
#Customized Email verification error message
javax.validation.constraints.email.Message = Please enter a valid email address
#Custom NoteMPTY verification error message
javax.validation.constraints.noteMPTY.MESSAGE = Fields cannot be empty
2. Question: How to perform verification on nested objects?
solution:
In the Java EE application, it is often encountered that the nested object is required to perform verification.The Java Ee Validation framework provides @Valid annotations, which can be used to perform verification on the nested object.The example code is as follows:
public class Address {
@Notempty (MESSAGE = "The street cannot be empty")
private String street;
@Notempty (MESSAGE = "City cannot be empty")
private String city;
// omit the getter and setter method
}
public class User {
@Notempty (MESSAGE = "Name cannot be empty")
private String name;
@Valid
private Address address;
// omit the getter and setter method
}
public class Main {
public static void main(String[] args) {
User user = new User();
user.setName ("Zhang San");
// Create a user object with an empty address
Address address = new Address();
user.setAddress(address);
// Execute verification
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
// Output verification error message
for (ConstraintViolation<User> violation : violations) {
System.out.println(violation.getMessage());
}
}
}
3. Question: How to perform verification at the controller layer?
solution:
In Java EE applications, verification is usually performed in the controller layer (such as the Servlet or REST controller).It can be verified by defining a verification class and using @valid annotations in the controller method.The example code is as follows:
import javax.validation.Valid;
@Path("/users")
public class UserController {
@Inject
private UserService userService;
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createUser(@Valid User user) {
userService.createUser(user);
return Response.ok().build();
}
}
In the above code, the Createuser method uses @Valid annotations to perform verification.If the verification fails, the constraintviolationException will be thrown out and return the corresponding error message.
in conclusion:
This article introduces some techniques and skills to solve the common problems of Java Ee Validation framework.Through customized verification messages, executing verification on nested objects, and executing verification at the controller layer, developers can better apply the Java Ee Validation framework to ensure the integrity and accuracy of the data.