Powerful tactic to use exception mapper in Dropwizard

0
3446
Dropwizard exception mapper
Powerful tactic for exception mapper in Dropwizard
Learn on Udemy Today!

Problem:

When we develop RESTful web services, we will have some confusion for handling Exceptions. This post will explain about the powerful tactic for exception mapper with Dropwizard framework. JAX-RS specification has already introduced Exception mappers for handling such a situation in a great way. We will see how this problem was addressed in an effective way by using Dropwizard infrastructure.

How?

Now we will see how we can address this problem by seeing some real code with Dropwizard framework.

There are 3 steps involved for achieving this.

  1. Introduce a custom Exception
  2. Write a Exception Mapper for our newly introduced Exception
  3. Throw the custom Exception from our REST API

Custom Exception:

Exception Mapper:

REST API changes:

Finally register the provider instance (Exception Mapper) into Dropwizard application like below,

Our job is over! You will see the proper error message as response when you hit the API with error scenario. I hope this article has helped you to understand how we can use custom exception with REST API.

Please share the article with your friends and provider your comments below if any…

For detailed code example referĀ https://github.com/cloudskol/dropwizardskol

Thank you!

SHARE
Previous articleRapid REST API development with Dropwizard
Next articleTechnique to shield your API with @NameBinding
Thamizharasu is a Java developer living in India. He is writing about Java programming, RESTful web services and frameworks. He also loves working with Web API, Elastic search and Firebase. Currently he is a Madras JUG lead and you can follow him on Twitter @zarub2k