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)
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:
- add the following dependencies:
jersey-bean-validation
and jersey-mvc-jsp
- create an ExceptionMapper for ConstraintViolationException
- register your providers
Dependencies
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).
ExceptionMapper
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.
Registering Providers
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);