Jersey - Register ExceptionMapper for Resource Methods


The question <a href="https://stackoverflow.com/questions/36370991" rel="nofollow">Exception Handling/Mapping for a particular class</a> brought me to the question of how to register an ExceptionMapper to a particular resource <strong>Method</strong>.

I've tried to use a DynamicFeature like this:

<h3>DynamicFeature</h3> <pre class="lang-java prettyprint-override">@Provider public class ExceptionMapperDynamicFeature implements DynamicFeature { @Override public void configure(final ResourceInfo resourceInfo, final FeatureContext context) { if(!resourceInfo.getResourceMethod().isAnnotationPresent(BindExceptionMapper.class)) return; BindExceptionMapper bem = resourceInfo.getResourceMethod().getAnnotation(BindExceptionMapper.class); context.register(bem.mapper()); } } <h3>Annotation</h3> <pre class="lang-java prettyprint-override">@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface BindExceptionMapper { Class<? extends ExceptionMapper<?>> mapper() default WebApplicationExceptionMapper.class; } <h3>Resource</h3> <pre class="lang-java prettyprint-override">@GET @Produces(MediaType.APPLICATION_JSON) @BindExceptionMapper public Response test() { // do something that throws an exception }

The result was disillusioning:


WARNING: The given contract (interface javax.ws.rs.ext.ExceptionMapper) of class ...WebApplicationExceptionMapper provider cannot be bound to a resource method.


Then I was searching for other possibilities without luck and ended up with a AspectJ implementation, which you can see as a <a href="https://stackoverflow.com/questions/36370991/exception-handling-mapping-for-a-particular-class/36374188#36374188" rel="nofollow">part of my answer</a> to the linked question above.

So the complete question:

<strong><em>Is there a way to successfully register an ExceptionMapper to a resource Method?</em></strong>

And of course,

<ul><li>if yes then: how?</li> <li>if no then: why?</li> </ul>

I'm curious about the answers :)

<strong>Please notice:</strong><br /> This question is not about to register an ExceptionMapper to a resource <strong>Class</strong> like...

<pre class="lang-java prettyprint-override">public class ApplicationResourceConfig extends ResourceConfig { public ApplicationResourceConfig() { register(WebApplicationExceptionMapper.class, TestResource.class); } }

and especially not about to register them at all.


It's not <em>exactly</em> what you asked, but it's the only way I've found to do something like you want:

@Provider public class ErrorExceptionHandler implements ExceptionMapper<Throwable> { @Context private ResourceInfo resourceInfo; @Override public Response toResponse(final Throwable exception) { // this is an example of how to do something custom based on an annotation if(!resourceInfo.getResourceClass().isAnnotationPresent(SomeCustomAnnotation.class) && !resourceInfo.getResourceMethod().isAnnotationPresent(SomeCustomAnnotation.class)) return null; // do some custom thing here based on your annotation, maybe call another ExceptionMapper not annotated with @Provider ? return Response.status(500).entity("ERROR").build(); } }

It works for my purposes anyway.


  • Dynamic variables with given type
  • Maven is suddenly throwing error, Truncated class file
  • Why http authentication with htaccess get slow when password incorrect?
  • cdi produce method ignored
  • Deserialize to String or Object using Jackson
  • Missing NamedQueries annotation in WAS Liberty environment
  • sass parent of parent specificity
  • How to set value in annotated field in java?
  • Access annotation at runtime
  • Jersey - Register ExceptionMapper for Resource Methods
  • Data not passing correctly with wp_localize_script
  • Java Generics and Super Type Tokens
  • google map set styles dynamically of map object
  • How to create a constraint validator for multiple fields through annotation in spring 4.*
  • Question about pointer alignment
  • How can I make a circular menu icon? Please see details
  • Getting the parameterised type of a generic in swift?
  • Passing information to server-side function in a Google Docs Add On
  • Why are “sc.addFile” and “spark-submit --files” not distributing a local file to all workers?
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • runtime error when linking ffmpeg libraries in qt creator
  • Why can't UI components be accessed from a backgroundworker?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Detecting null parameter in preprocessor macro
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Sonar maven jacoco code coverage for Multimodule project
  • Firefox Extension - Monitor refresh and change of tab
  • Spring security and special characters
  • C# - Serializing and deserializing static member
  • Shallow update not allowed (git > 1.9)
  • Trying to switch camera back to front but getting exception
  • jquery mobile loadPage not working
  • How to include full .NET prerequisite for Wix Burn installer
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Qt: Run a script BEFORE make
  • Programmatically clearing map cache