We can easily validate a JAX-RS resource class field or method parameter using bean validation something like the following:
@Size(min = 18, max = 80, message = "Age must be between {min} and {max}.") String age;
What is the easiest way to bind the error message to a JSP page?
(Say, I am using Java EE 7 with Jersey or Resteasy)
Validation is the process of verifying that some data obeys one or more pre-defined constraints. It is, of course, a very common use case in most applications. The Java Bean Validation framework (JSR-380) has become the de-facto standard for handling this kind of operations in Java.
A validation error occurs when you have validation/response checking turned on for one of the questions and the respondent fails to answer the question correctly (for numeric formatting , required response).
To validate these entity classes, use the @Valid annotation on the method parameter. For example, the following class is a user-defined class containing both standard and user-defined validation constraints.
Hibernate Validator allows to express and validate application constraints. The default metadata source are annotations, with the ability to override and extend through the use of XML. It is not tied to a specific application tier or programming model and is available for both server and client application programming.
EDIT 1
We introduced new annotation @ErrorTemplate in Jersey 2.3 that covers this use-case. Handling JAX-RS and Bean Validation Errors with MVC describes it deeper and shows how to use it.
With Jersey you can follow these steps:
jersey-bean-validation
and jersey-mvc-jsp
If you're using Maven you can simply add these dependencies to your pom.xml
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.1</version>
</dependency>
otherwise refer to the modules dependency pages to get a list of required libraries (jersey-mvc-jsp and jersey-bean-validation).
Bean Validation runtime throws a ConstraintViolationException when something goes wrong during validation an entity (or JAX-RS resource). Jersey 2.x provides a standard ExceptionMapper to handle such exceptions (ValidationException
to be precise) so if you want to handle them differently, you need to write an ExceptionMapper of your own:
@Provider
@Priority(Priorities.USER)
public class ConstraintViolationExceptionMapper implements ExceptionMapper<ConstraintViolationException> {
@Override
public Response toResponse(final ConstraintViolationException exception) {
return Response
// Define your own status.
.status(400)
// Put an instance of Viewable in the response so that jersey-mvc-jsp can handle it.
.entity(new Viewable("/error.jsp", exception))
.build();
}
}
With the ExceptionMapper above you'll be handling all thrown ConstraintViolationExceptions and the final response will have HTTP 400
response status. Entity passed (Viewable) to the response will be processed by MessageBodyWriter from jersey-mvc
module and it will basically output a processed JSP page. The first parameter of the Viewable class is a path to a JSP page (you can use relative or absolute path), the second is the model the JSP will use for rendering (the model is accessible via ${it}
attribute in JSP). For more on this topic, refer to the section about MVC in Jersey Users guide.
The last step you need to make is to register your providers into your Application (I'll show you an example using ResourceConfig from Jersey which extends Application class):
new ResourceConfig()
// Look for JAX-RS reosurces and providers.
.package("my.package")
// Register Jersey MVC JSP processor.
.register(JspMvcFeature.class)
// Register your custom ExceptionMapper.
.register(ConstraintViolationExceptionMapper.class)
// Register Bean Validation (this is optional as BV is automatically registered when jersey-bean-validation is on the classpath but it's good to know it's happening).
.register(ValidationFeature.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With