【问题标题】:JAX-RS Exception Mapper not working in Grizzly containerJAX-RS 异常映射器在 Grizzly 容器中不起作用
【发布时间】:2016-11-01 02:10:02
【问题描述】:

与团队一起开发 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: 基于this 问题,我尝试使用此ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"property,它只提供了一点帮助。发生异常时,我仍然会看到 html grizzly 页面,但现在我在页面正文中看到了我的异常(+堆栈跟踪)。

【问题讨论】:

  • 映射器可能没有被注册。你能展示你的应用设置/配置吗?
  • 您是指启动服务器的代码吗?依赖项?
  • 只是服务器代码,至少显示ResourceConfig
  • 我不是迁移的一部分,所以我不确定这是否是你要问的,我将编辑导入 org.glassfish.jersey.server.ResourceConfig 的类的问题;
  • 你没有展示使用这个类。重要信息在 ResourceConfig 的创建中

标签: exception-handling jax-rs jersey-2.0 grizzly exceptionmapper


【解决方案1】:

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

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")

【讨论】:

  • 谢谢,我会试试看 :) 当我得到它的工作时,我会接受你的回答
  • 这对我有用,遇到了同样的问题,注册映射器后,我能够得到映射的异常。谢谢你的回答
猜你喜欢
  • 2011-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
相关资源
最近更新 更多