为什么我的ExceptionMapper会在Jersey / Grizzly被抛弃?

时间:2015-01-20 17:55:05

标签: java jersey jax-rs grizzly

目前我在Grizzly 2.3.16和Jersey 2.14工作。我试图获取服务器处理的自定义异常并返回给客户端。相反,我似乎得到了通用的Grizzly错误,即使似乎调用了正确的代码。

这是过滤方法(具有' @ Provider'装饰的类的一部分)。

@Override
public void filter(ContainerRequestContext context) throws IOException 
{
    throw new MappableException(
        new CustomAuthenticationException(
            "Custom Error message"));
}

以下是整个ExceptionMapper实现:

@Provider
public class CustomAuthenticationExceptionMapper
    implements ExceptionMapper<CustomAuthenticationException>
{
    public Response toResponse(CustomAuthenticationException e)
    {
        return Response.status(Status.UNAUTHORIZED.getStatusCode()).
                header("WWW-Authenticate", "Basic realm=\"Custom\"").
                type("text/plain").entity(e.getMessage()).build();
    }
}

以下是我如何得到回复并阅读它:

response = invocationBuilder.method(requestMethod.toString(), Response.class);
System.out.println(response.readEntity(String.class));

当我获得实体时,它是来自Grizzly的HTML,而不是我应该的自定义消息。

以下是调用toResponse之前的堆栈跟踪 - 我可以验证代码是否被调用但看起来不知何故泽西或Grizzly只是丢弃它并最终被默认的灰熊消息替换。

at org.glassfish.jersey.server.ServerRuntime$Responder.mapException(ServerRuntime.java:528)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:409)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:297)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:254)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1030)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:377)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    at org.glassfish.grizzly.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:147)
    at org.glassfish.grizzly.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:106)
    at org.glassfish.grizzly.servlet.ServletHandler.doServletService(ServletHandler.java:221)
    at org.glassfish.grizzly.servlet.ServletHandler.service(ServletHandler.java:169)
    at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:219)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Unknown Source)

我不确定其他部分是否相关,但可以根据需要提供。

1 个答案:

答案 0 :(得分:1)

我需要从maven添加jersey-grizzly-connector.jar。