Over a million developers have joined DZone.

Java EE 8 MVC: Global Exception Handling

Adding global exception handling to an Java EE MVC application is quite simple.

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

In the previous posts we learned about various ways to access request information (e.g. query or path parameters) in Java EE MVC. This post shows how to apply global exception handling to an MVC application.

Assume we have a controller method that might throw an IllegalArgumentException:

@Controller
@Path("illegal-argument")
public class ExceptionController {

  @GET
  public String doWork() {
    // code that might throw an IllegalArgumentException
  }

}


We could now add a try/catch block to doWork() and wrap the piece of code that might throw the exception. However, this approach becomes tedious if it needs to be applied to multiple methods.

In such a case we can register a global exception mapper. To do this, we have to create a class that implements the generic ExceptionMapper interface.

A simple ExceptionMapper for IllegalArgumentExceptions looks like this:

@Provider
public class IllegalArgumentExceptionMapper implements ExceptionMapper<IllegalArgumentException> {

  @Inject
  private Models models;

  @Override
  public Response toResponse(IllegalArgumentException exception) {
    models.put("message", exception.getMessage());

    return Response.status(Response.Status.BAD_REQUEST)
        .entity("/WEB-INF/jsp/error.jsp")
        .build();
  }
}


Now, whenever an IllegalArgumentException is thrown from controller methods, IllegalArgumentExceptionMapper will be used to convert the exception to an appropriate response. Here a simple error view (error.jsp) is rendered.

If you want a generic ExceptionMapper that handles all types of exceptions, you simply have to implement ExceptionMapper<Exception>. If you have multiple ExceptionMapper implementations that are suitable to handle a thrown exception, the most specific ExceptionMapper is used.

Quick Summary

Adding global exception handling to an Java EE MVC application is quite simple. We only have to create a class that implements the ExceptionMapper interface with the exception type that should be handled.

The full example code can be found on GitHub.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:
mvc ,java ee 8

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}