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)
Best Answer
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
andjersey-mvc-jsp
Dependencies
If you're using Maven you can simply add these dependencies to your
pom.xml
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: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 fromjersey-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):