Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding JAX-RS bean validation error messages to the view

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)

like image 612
siva636 Avatar asked Aug 02 '13 11:08

siva636


People also ask

Which of the following is type of bean validation method provided in Jersey?

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.

What is err validation?

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).

How will you enable validation on the resource in the following bean class?

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.

How does Hibernate Validator work?

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.


1 Answers

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:

  1. add the following dependencies: jersey-bean-validation and jersey-mvc-jsp
  2. create an ExceptionMapper for ConstraintViolationException
  3. 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);
like image 57
Michal Gajdos Avatar answered Sep 22 '22 05:09

Michal Gajdos