Resteasy does not find ExceptionMapper provider

resteasy exception mapper
exceptionmapper spring boot
resteasy client exception handling
jersey client exception handling
exceptionmapper not working
webapplicationexception
resteasyproviderfactory
json exception mapper

I have tried a lot of things to make this work, but it is not working anyway. Also, I don't find proper documentation about this. I am trying to implement a CustomExceptionMapper for my CustomException type. The CustomException is thrown correctly, but it is not catched.

I thought that annotating the CustomExceptionMapper with @Provider was enough, but it isnot detected. I have tried to allow scanning in web.xml, but I've found this: https://issues.jboss.org/browse/AS7-1739 . I am using 7.0.1 Final. Probably the solution is changing of version, but this decision is not up to me.

I have also found that you can try to override the getClasses() or getSingletons method and add there your CustomExceptionMapper, but it is just not detecting it.

How my Application class looks like:

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;


@ApplicationPath("/service")
public class MyApp extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(TestExceptionMapper.class); 
        return classes;

    }
}

And the Mapper

@Provider
public class TestExceptionMapper implements ExceptionMapper<TestException> {

    public Response toResponse(TestException ex) {
       //something, but I cannot reach it.
    }
}

Web.xml

 <context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>com.mycompany.rest.exceptions.TestExceptionMapper</param-value>        
</context-param>


<servlet>
    <servlet-name>RestletServlet</servlet-name>
    <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
    <init-param>
        <param-name>org.restlet.application</param-name>
        <param-value>com.mycompany.rest.application.myApplication</param-value>
    </init-param>
</servlet>

And the service which is being called through URL:

@Stateless
@Path("/somePath")
public class someService {

    @GET //this should be post, but for testing I'm using get
    @Path("/some/update/someth")
    @Produces()
    public String updateSometh(@NotNull @QueryParam("key") String key, @QueryParam(value = "somethID") Long somethID) throws TestException {
       // ....
    }

If you call this with the correct parameters, it works. But if you put a bad parameter, it does not. I would like to say too that the application is working, but I just wanted to add the Mappers.

Exception mappers works for the exceptions thrown from the body of your resource method. If you modify your resource to throw a TestException:

public String updateSometh(@NotNull @QueryParam("key") String key, 
            @QueryParam(value = "somethID") Long somethID) throws TestException {
   throw new TestException();
}

the mapper should catch the exception. The problem is in the way you tested.

Chapter 27. Exception Handling, ExceptionMappers are custom, application provided, components that can catch By scanning, through the resteasy provider context-param (if you're deploying via a WAR file), You can find them in RESTEasy's javadoc under the package org.jboss.resteasy.spi. MethodNotAllowedException, 405, Method Not Allowed. ExceptionMapper is a contract for a provider that maps Java exceptions to Response object. An implementation of ExceptionMapper interface must be annotated with @Provider to work correctly. 1. Resteasy ExceptionMapper – Custom exception handler. A sample implementation provider class of ExceptionMapper looks like this:

does your web.xml have the context-param for resteasy providers?

something like this

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>packagename.TestExceptionMapper</param-value>
</context-param>

RESTEasy ExceptionMapper, ExceptionMapper is a contract for a provider that maps Java exceptions to Response object. Resteasy ExceptionMapper – Custom exception handler. A sample package com.howtodoinjava.rest;. import javax.ws.rs.GET;. import javax.ws.rs.Path; throw new MyApplicationException( "id is not present in request !!" );. RESTEasy provides such a exception handling mechanism which simplifies the exception handling process. RESTEasy ExceptionMappers are custom, application provided, components that can catch thrown application exceptions and write specific HTTP responses. The are classes annotated with @Provider and that implement this interface.

You can also register a custom ExceptionMapper in your javax.ws.rs.core.Application class by overriding the classes() method like so:

@ApplicationPath("/api")
public class RestApplication extends Application {

  @Override
  public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<>();
    // other resource classes ...

    classes.add(CustomExceptionMapper.class);
    return classes;
  }
}

The ExceptionMapper itself can use java.lang.Exception as Type parameter to handle any kind of exception.

@Provider
public class CustomExceptionMapper implements ExceptionMapper<Exception> {
    //...
}

Defining exception mappers for resource exceptions and errors, If you cannot throw the exception, WebApplicationException , in your code The following example illustrates a simple ExceptionMapper class: If you rely on the annotation scanning capabilities to find all of your JAX-RS  I customized my own ExceptionMapper to deal with all Throwable, and by the way I have tested that it works in most cases. But when there is an exception caught in SynchronousDispatcher(I just track step by step), it does not go to my ExceptionMapper and just print stack info into console. The exception is:

[RESTEASY-377] Regression: Exception Mapper cannot be , The exception mapper: @Provider public class KundeValidationExceptionMapper implements ExceptionMapper<​KundeValidationException> { private static  Just try to have an ExceptionMapper for ConstraintViolationException, then add an endpoint that fails validation (Bean Validation) and then you will see that your ExceptionMapper is never called (because ResteasyViolationExceptionMapper gets it instead).

JAX-RS ExceptionMappers are never invoked · Issue #1994 , It is no longer called in 0.13.1: @Provider public class IllegalArgumentExceptionMapper Not a regression but a bugfix in RESTEasy - see  Not all RESTEasy modules are bundled with WildFly. For example, resteasy-fastinfoset-provider and resteasy-wadl are not included among the modules listed in Section 3.1, “RESTEasy modules in WildFly”. If you want to use them in your application, you can include them in your WAR as you would if you were deploying outside of WildFly.

RESTEasy Exception Handling with ExceptionMapper, The following is a example of ExceptionMapper implements. package org.jboss.​resteasy.examples; import java.io. see FilterDispatcher and ServletDispatcher behave differently, If use FilterDispatcher, Http Error Code can not be mapped. In this section we will find the the reson for why behave differently. Resteasy does not provide any builtin ExceptionMappers. This means in the scenario you describe there must be a custom ExceptionMapper that has been register and

Comments
  • Where is your code that throws TestException?
  • I am testing what happens when I put a Long parameter with a wrong value (like 120sd instead of just a number). The exception is thrown there, because it cannot be parsed.
  • Please show the resource code where are you doing this test. I suspect is a problem there as I have a very similar configuration to yours and exception mappers work fine.
  • Well, that's good to know. But anyway, isn't it possible to catch exceptions that do not even arrive to the body?
  • These exceptions might have different causes that you usually can deal with. For example, in your case you can use a ValidatorAdaptor to validate the somethID parameter. I think you also need to make it a String instead of a Long.
  • I will try with the validator approach, but having to parse by myself seems not a good approach when if can do it automatically
  • In fact the ValidatorAdaptor is suitable for more complex validations, maybe in this simple case is better to let Resteasy return the default Bad Request response.
  • if RESTeasy is configured to scan all resources, is it necessary to have context-param?
  • how can I register several Exception Mappers for different type of Exception in RestEasy?