Search code examples
javarestjax-rsresteasyprovider

Resteasy does not find ExceptionMapper provider


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.


Solution

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