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

这是filter方法(具有'@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之前的堆栈跟踪-我可以验证代码是否已被调用,但是看起来Jersey或Grizzly某种程度上只是丢弃了它,最终被默认的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)


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

最佳答案

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

关于java - 为什么我的ExceptionMapper丢在Jersey/Grizzly中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28052023/

10-10 23:22