How to use Jersey Ext Bean Validation framework
Jersey Ext Bean Validation is an extension framework for BEAN verification in Jersey.It is based on Java's Bean Validation specifications and can help us easily achieve verification and verification of input data in the RESTFUL service.This article will introduce how to use the Jersey Ext Bean Validation framework in Jersey and provide some Java code examples.
# 引 引
First, we need to add Jersey Ext Bean Validation to the project's pom.xml file.Add the following in the <DependenCies> tag:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.34</version>
</dependency>
# Register Bean Validation function
Next, register the Bean Validation function in the launching class.You can create a class that inherits `ORG.GLASSFISH.JERSEY.Server.RSourceConfig`, and register` org.glassfish.jersey.Validation Feature`:
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.validation.ValidationFeature;
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("your.resource.package");
register(ValidationFeature.class);
}
}
Make sure the `youR.Resource.package` is replaced with the actual resource package path.
# Verification in the resource class
Now, we can use the Bean Validation annotation on the method parameters of the resource class to define the verification rules.The following is an example:
import javax.validation.constraints.NotNull;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
@Path("/example")
public class ExampleResource {
@POST
public void exampleMethod(@NotNull @QueryParam("param") String param) {
// Execution method logic
}
}
In the above example, we apply the `@notnull` annotation to the` param` query parameter to ensure that the parameter cannot be empty.If there is no `param` parameter in the request, Jersey will return an error message with 400 Bad Request response.
# Error treatment
If the verification fails, the Jersey Ext Bean Validation framework will automatically handle the error and return the corresponding error message.We can write a format of an abnormal processor of the `ORG.glassfish.jersey.Server. Validation.ValidationExceptionMapper` interface.
The following is a simple error processor example:
import org.glassfish.jersey.server.validation.ValidationExceptionMapper;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class CustomValidationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(ConstraintViolationException exception) {
StringBuilder message = new StringBuilder();
for (ConstraintViolation<?> violation : exception.getConstraintViolations()) {
message.append(violation.getMessage()).append("
");
}
return Response.status(Response.Status.BAD_REQUEST).entity(message.toString()).build();
}
}
In the above examples, we traversed all the verification violations and added the error message to a StringBuilder.
# Summarize
This article introduces how to use the Jersey Ext Bean Validation framework in Jersey for BEAN verification.We can implement data verification and verification by introducing dependencies, registering the Bean Validation function, and adding verification rules to the resource class.At the same time, we also provide an example of a custom error processor to help us customize the error message format when verification failure.