Jersey - 在ContainerRequestFilter范围内获取远程地址

时间:2014-11-29 00:27:42

标签: java jersey

我试图使用this question的代码,每次我向应用程序发出请求时都会收到此错误:

HTTP状态500 - Servlet的Servlet.init()api.tienda.venta.ApplicationConfig抛出异常

堆栈追踪:

javax.servlet.ServletException: Servlet.init() for servlet api.tienda.venta.ApplicationConfig threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

这是我正在使用的代码:

//ApplicationConfig.java

@ApplicationPath("venta")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(JacksonFeature.class);
        resources.add(RequestFilter.class);
        resources.add(api.tienda.venta.ventaResource.class);
    }

}


// RequestFilter.java 

public class RequestFilter implements ContainerRequestFilter{

    @Context
    private HttpServletRequest request;

    @Override
    public void filter(ContainerRequestContext requestContext) {

      String remoteAddr = request.getRemoteAddr();

      System.out.print("Remote Address: " + remoteAddr);
    }

}

如果我删除 @Context 注释,错误就会消失,一切正常,任何有关此处发生的事情的线索?

0 个答案:

没有答案