PreProcessInterceptor不拦截我的一些JAX-RS资源服务

时间:2013-05-03 10:16:54

标签: java jax-rs resteasy interceptor

pom.xml:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>jaxrs-api</artifactId>
    <version>2.3.1.GA</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jaxrs</artifactId>
    <version>2.3.1.GA</version>
</dependency>

拦截器类:

@Provider
@ServerInterceptor
@SecurityPrecedence
public class AuthorizationInterceptor implements PreProcessInterceptor {
    @Override
    public ServerResponse preProcess(HttpRequest request, ResourceMethod method) throws Failure, WebApplicationException {
        // some initalizations
    }
}

未拦截网络服务:

@Path("/poi")
@Consumes({"application/xml", "application/json", "application/octet-stream"})
@Produces({"application/json", "text/json", "application/xml"})
@Restricted
public class PoiResource extends SecureResource<Poi> {

    @Inject
    public PoiRessource(PoiDao pois) {
        super(pois);
    }

    @GET
    @Path("/ping")
    @UnRestricted
    public Response ping() {
        String date = new Date().toString();
        if (log.isInfoEnabled()) log.info(String.format("Ping %s",date));
            return Response.ok(date).build();
    }
}

拦截了网络服务:

@Path("/access")
@Consumes({"application/xml", "application/json", "application/octet-stream",  "application/x-www-form-urlencoded"})
@Produces({"application/json", "text/json", "application/xml"})
@Restricted
public class AccessResource extends SecureResource<Access> {

    @Inject
    public AccessResource(AccessDao accessDao) {
        super(accessDao);
    }

    @GET
    @Path("/ping")
    @UnRestricted
    public Response ping() {
        String date = new Date().toString();
        if (log.isInfoEnabled()) log.info(String.format("Ping %s",date));
            return Response.ok(date).build();
    }
}

当我在PoiResource上调用ping服务时出现异常:

2013-05-03 18:34:09.973 [http-9080-3] ERROR c.c.a.r.p.exception.ExceptionMapper - 
com.google.inject.ProvisionException: Guice provision errors:

1) Error injecting constructor, java.lang.NullPointerException
at com.connecthings.adtag.common.dao.PoiDao.<init>(PoiDao.java:69)
while locating com.connecthings.adtag.common.dao.PoiDao
for parameter 0 at com.connecthings.adtag.poi.rest.PoiRessource.<init>(PoiRessource.java:98)
while locating com.connecthings.adtag.poi.rest.PoiRessource

1 error
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987) ~[guice-3.0.jar:na]
    at com.connecthings.adtag.common.inject.ConfigurableRestEasyGuiceListener$1.createResource(ConfigurableRestEasyGuiceListener.java:104) ~[common-inject-4.0.0-SNAPSHOT.jar:na]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:210) ~[resteasy-jaxrs-2.3.1.GA.jar:na]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.1.GA.jar:na]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.1.GA.jar:na]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.1.GA.jar:na]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.1.GA.jar:na]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.1.GA.jar:na]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.1.GA.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) [servlet-api.jar:na]
    at com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:263) [guice-servlet-3.0.jar:na]
    at com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:178) [guice-servlet-3.0.jar:na]
    at com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:91) [guice-servlet-3.0.jar:na]
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:62) [guice-servlet-3.0.jar:na]
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:118) [guice-servlet-3.0.jar:na]
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:113) [guice-servlet-3.0.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) [catalina.jar:6.0.35]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) [catalina.jar:6.0.35]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) [catalina.jar:6.0.35]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [catalina.jar:6.0.35]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [catalina.jar:6.0.35]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [catalina.jar:6.0.35]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [catalina.jar:6.0.35]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) [catalina.jar:6.0.35]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) [tomcat-coyote.jar:6.0.35]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) [tomcat-coyote.jar:6.0.35]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) [tomcat-coyote.jar:6.0.35]
    at java.lang.Thread.run(Thread.java:679) [na:1.6.0_27]
Caused by: java.lang.NullPointerException: null
    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:796) ~[na:1.6.0_27]
    at com.mongodb.Mongo.getDB(Mongo.java:362) ~[mongo-java-driver-2.10.1.jar:na]
    at com.google.code.morphia.DatastoreImpl.<init>(DatastoreImpl.java:77) ~[morphia-0.99.jar:na]
    at com.google.code.morphia.DatastoreImpl.<init>(DatastoreImpl.java:88) ~[morphia-0.99.jar:na]
    at com.connecthings.adtag.model.dao.UnsecureGenericDao.getDs(UnsecureGenericDao.java:74) ~[common-persistence-4.0.0-SNAPSHOT.jar:na]
    at com.connecthings.adtag.model.dao.UnsecureGenericDao.getCollection(UnsecureGenericDao.java:112) ~[common-persistence-4.0.0-SNAPSHOT.jar:na]
    at com.connecthings.adtag.common.dao.PoiDao.<init>(PoiDao.java:73) ~[common-dao-4.0.0-SNAPSHOT.jar:na]
    at com.connecthings.adtag.common.dao.PoiDao$$FastClassByGuice$$3bbdc31e.newInstance(<generated>) ~[guice-3.0.jar:na]
    at com.google.inject.internal.cglib.reflect.$FastConstructor.newInstance(FastConstructor.java:40) ~[guice-3.0.jar:na]
    at com.google.inject.internal.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:60) ~[guice-3.0.jar:na]
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:85) ~[guice-3.0.jar:na]
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254) ~[guice-3.0.jar:na]
    at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38) ~[guice-3.0.jar:na]
    at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:62) ~[guice-3.0.jar:na]
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:84) ~[guice-3.0.jar:na]
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254) ~[guice-3.0.jar:na]
    at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:978) ~[guice-3.0.jar:na]
    at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1024) ~[guice-3.0.jar:na]
    at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:974) ~[guice-3.0.jar:na]
    ... 27 common frames omitted

我无法找出为什么拦截了AccessResource的ping方法而且PoiResource的ping方法不是...... 抛出异常是因为poi dao需要在实例化的拦截器方法中初始化的对象。

如果有人对我有任何线索,那将会有很大帮助。 谢谢!

1 个答案:

答案 0 :(得分:1)

您的异常表明该问题来自PoiDao的构造函数,阻止PoiResource构建。

在resteasy中,在资源实例化之后和资源方法调用之前调用拦截器方法。

由于您的资源无法实现,因此永远不会调用拦截器。