简体   繁体   中英

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.

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>

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.

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> {
    //...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM