JAX-RS异常映射器在Grizzly容器中不起作用

佐尔坦循环

随着项目的规模越来越大,与团队一起工作在Jersey Web应用程序上,我们决定从Tomcat切换到Grizzly,以允许将项目的一部分部署在不同的端口号上。我现在发现,我们拥有的自定义异常处理现在无法正常工作,相反,我总是得到灰白的html页面。

示例异常:

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
 }
}

映射器:

@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
 }

}

我不确定问题出在哪里,如果需要,我可以提供更多的信息/代码。有什么问题,我该怎么办?

编辑

主类:

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:基于问题,我尝试使用此ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"属性,但仅提供了一点帮助。发生异常时,我仍然会看到html grizzly页面,但是现在我在页面的主体中看到了我的异常(+堆栈跟踪)。

保罗·萨姆索塔

您只需要为整个应用程序注册一个资源类

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

映射器也需要注册

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

您也可以使用包扫描,如果使用@Path注释,它将扫描所有类并自动注册它们。@Provider

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章