toResponse in jersey ExceptionMapper does not get

2019-01-24 06:07发布

So I'm building a web application, we are using JPA and Jersey to consume/produces JSON data.

I have a custom "EntityException" aswell as a custom "EntityExceptionMapper"

Here's the mapper:

  @Provider
public class EntityExceptionMapper implements ExceptionMapper<EntityException> {

    public EntityExceptionMapper() {
        System.out.println("Mapper created");
    }

    @Override
    public Response toResponse(EntityException e) {
        System.out.println("This doesnt print!");
        return Response.serverError().build();
    }
}

My Exception:

public class EntityException extends Exception implements Serializable{

  public EntityException(String message) {
      super(message);
      System.out.println("This prints...");
  }

}

And I'm calling it from a REST call:

@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String test() throws EntityException{
    throw new EntityException("This needs to be send as response!!");
    //return "test";
}

My problem is that, when the above exception is thrown, I get in the constructor (prints: "This prints...") Edit: I also get the: "Mapper created!"

But my response is empty, and I don't get to the sys out of my toResponse method. This is really similar to the example on the jersey website:

https://jersey.java.net/nonav/documentation/1.12/jax-rs.html#d4e435

What am I missing??

10条回答
走好不送
2楼-- · 2019-01-24 07:05

I am still using jersey 1.17 , spring and jersy-spring

@Component annotation fixes this

查看更多
放荡不羁爱自由
3楼-- · 2019-01-24 07:06

I had the same problem and was able to fix it by including the package of my ExceptionMapper in the jersey.config.server.provider.packages in my web.xml file. Below is a snippet from my web.xml.

<servlet>
    <servlet-name>voteride-servlet</servlet-name>
    <servlet-class>
        org.glassfish.jersey.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            com.voteride.ws;com.voteride.errorHandling;org.codehaus.jackson.jaxrs
        </param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.scanning.recursive</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
查看更多
Root(大扎)
4楼-- · 2019-01-24 07:09

I had a similar problem where the ExceptionMapper had the proper @Provider annotation and the rest of the code was identical to Jersey's example but still wasn't registered properly.

Well it turns out I had to register manually my custom ExceptionMapper within my HttpServlet with the method addExceptionMapper. Because it's now manually registered, the @Provider annotation can be safely removed.

So with the following ExceptionMapper (I'm catching every RuntimeException to rethrow them as 400)

public class MyCustomExceptionHandler implements ExceptionMapper<RuntimeException> {

  @Override
  public Response toResponse(RuntimeException exception) {
    return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
  }
}

I had to add the 2nd line in my init :

HttpServlet serviceServlet = jerseyServletFactory.create(someResource);
jerseyServletFactory.addExceptionMapper(new MyCustomExceptionHandler()); //<--

httpServer.register(serviceServlet, "/api");
httpServer.start();
查看更多
老娘就宠你
5楼-- · 2019-01-24 07:11

Try to register your exception mapper class in your X extends ResourceConfig file. register(CustomExceptionMapper.class); this line will help application to find your mapper class and return whatever you have written inside the toResponse method of mapper class

查看更多
登录 后发表回答