@NameBinding导致org.glassfish.jersey.server.spring.scope.RequestContextFilter $ 2.resetAttributes

时间:2017-11-21 08:43:54

标签: jax-rs servlet-filters

我试图在我的JAX-RS项目中添加一个名为ContainerRequestFilter的东西。 首先,我创建了一个注释:

@NameBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface IsAuthenticated {}

然后,我创建了提供者:

@PreMatching
@Provider
@IsAuthenticated
public class AuthenticationRequestFilter implements ContainerRequestFilter {

    public void filter(ContainerRequestContext requestContext) throws IOException {
        System.out.println("filter() on     ServerAuthenticationRequestFilter");
        Response.ResponseBuilder responseBuilder = Response.serverError();
        Response response = responseBuilder.status(Response.Status.BAD_REQUEST).build();
        requestContext.abortWith(response);
    }
}

然后,我在我的JAX-RS方法上使用它:

@GET
@Path("/trial")
@IsAuthenticated
public String trial() {
    return "Hello world";
}

不幸的是,这会因以下错误而崩溃:

  

StandardWrapperValve [SpringApplication]:Servlet的Servlet.service()   SpringApplication抛出了异常java.lang.NullPointerException   org.glassfish.jersey.server.spring.scope.RequestContextFilter $ 2.resetAttributes(RequestContextFilter.java:125)     在   org.glassfish.jersey.server.spring.scope.RequestContextFilter.filter(RequestContextFilter.java:138)     在   org.glassfish.jersey.server.ContainerFilteringStage $ ResponseFilterStage.apply(ContainerFilteringStage.java:196)     在   org.glassfish.jersey.server.ContainerFilteringStage $ ResponseFilterStage.apply(ContainerFilteringStage.java:163)     在   org.glassfish.jersey.process.internal.Stages.process(Stages.java:171)     在   org.glassfish.jersey.server.ServerRuntime $ Responder.processResponse(ServerRuntime.java:393)     在   org.glassfish.jersey.server.ServerRuntime $ Responder.process(ServerRuntime.java:385)     在   org.glassfish.jersey.server.ServerRuntime $ 1.run(ServerRuntime.java:280)     在org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:272)at at   org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:268)at   org.glassfish.jersey.internal.Errors.process(Errors.java:316)at   org.glassfish.jersey.internal.Errors.process(Errors.java:298)at   org.glassfish.jersey.internal.Errors.process(Errors.java:268)at   org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)     在   org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)     在   org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)     在   org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)     在   org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)     在   org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)     在   org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)     在   org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)     在   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591)     在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:463)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)     在   org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)     在   org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)     在   org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)     在   org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)     在   org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)     在   org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)     在   org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)     在   org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)     在   org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)     在   org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56)     在   org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)     在   org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:593)     在   org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:573)     在java.lang.Thread.run(Thread.java:748)

但是,如果我从提供程序中删除以下行:

requestContext.abortWith(response);

然后崩溃消失(虽然这没有按预期工作,因为永远不会返回错误的请求)。 我在web.xml上添加了:

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.trial.filters</param-value>
</init-param>
<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>com.trial.filters.AuthenticationRequestFilter</param-value>
</init-param>

发生了什么事?我做错了什么?

非常感谢

1 个答案:

答案 0 :(得分:0)

这个问题是由@PreMatching引起的,虽然我不知道为什么。但是,我已经删除了该注释,现在它正在按预期工作。