exceptionjax-rsjersey-2.0grizzlyexceptionmapper

JAX-RS Exception Mapper not working in Grizzly container


Working on a Jersey web application with a team, as the project got bigger and bigger, we decided to switch from Tomcat to Grizzly to allow deploying parts of the project on different port numbers. What I've found out now, that the custom exception handling we have fails to work now, instead I always get the grizzly html page.

Example exception:

public class DataNotFoundException extends RuntimeException{

private static final long serialVersionUID = -1622261264080480479L;

public DataNotFoundException(String message) {
    super(message);
    System.out.println("exception constructor called"); //this prints
 }
}

Mapper:

@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>{

public DataNotFoundExceptionMapper() {
    System.out.println("mapper constructor called"); //doesnt print
}

@Override
public Response toResponse(DataNotFoundException ex) {
    System.out.println("toResponse called"); //doesnt print
    ErrorMessage errorMessage = new ErrorMessage(ex.getMessage(), 404, "No documentation yet."); 
    return Response.status(Status.NOT_FOUND)
            .entity(errorMessage)
            .build();
      //ErrorMessage is a simple POJO with 2 string and 1 int field
 }

}

I'm not sure where is the problem source, if needed I can provide more information/code. What's the problem, what can I try?

EDIT:

Main.class:

public class Main {

/**
 * Main method.
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {

...

    List<ServerInfo> serverList = new ArrayList<ServerInfo>();

    serverList.add(new ServerInfo(
            "api",8450,
            new ResourceConfig().registerClasses(
                    the.package.was.here.ApiResource.class)
            ));             

    for(ServerInfo server : serverList) {
        server.start();
    }

    System.out.println("Press enter to exit...");
    System.in.read();

    for(ServerInfo server : serverList) {
        server.stop();
    }

}
}

EDIT2: based on this question I've tried using this ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"property, which only helped a little. I still get the html grizzly page when the exception happens, but now I see my exception (+stack trace) in the body of the page.


Solution

  • You're only registering one resource class for the entire application

    new ResourceConfig().registerClasses(
            eu.arrowhead.core.api.ApiResource.class
    )
    

    The mapper needs to be registered also

    new ResourceConfig().registerClasses(
            eu.arrowhead.core.api.ApiResource.class,
            YourMapper.class)
    )
    

    You can also use package scanning, which will pick up all classes and automatically register them, if they are annotated with @Path or @Provider

    new ResourceConfig().packages("the.packages.to.scan")