有没有办法记录每个Web服务调用来到泽西岛的应用程序

时间:2014-03-25 14:33:13

标签: java jersey-1.0

我在“我的应用程序”中使用 Jersey 1.8 。 现在,有很多服务类。 有没有办法记录所有来自不同服务的请求。 这意味着,是否可以创建类似于通用过滤器的东西,通过该过滤器可以访问所有请求,从中我可以跟踪并记录所有传入的请求。

3 个答案:

答案 0 :(得分:3)

是的,这很容易:

创建提供者,例如GenerelAccessHandler.java

@Provider
public final class GeneralAccessHandler implements ContainerRequestFilter, ContainerResponseFilter {

    /** The incoming request we obtained. */
    @Context
    private final HttpServletRequest mHttpServletRequest;

    /** Some useful informations about the accesspoint in our service. */
    @Context
    private final UriInfo mUriInfo;

    @Override
    public ContainerRequest filter(final ContainerRequest request) {
        if (null != mHttpServletRequest) {
            System.out.println("Method: " + mHttpServletRequest.getMethod());
            System.out.println("Session: " + mHttpServletRequest.getSession().getId());
            System.out.println("User Agent: " + mHttpServletRequest.getHeader("user-agent"));
            final String addr = mHttpServletRequest.getRemoteAddr();
            final int port = mHttpServletRequest.getRemotePort();
            System.out.println("Client: " + addr + ":" + port);
        }
        if (null != mUriInfo) {
            System.out.println("URI: " + mUriInfo.getRequestUri().getPath());
        }
        return request;
    }

    @Override
    public ContainerResponse filter(final ContainerRequest pReq, final ContainerResponse pResp) {
        System.out.println("Outgoing Response")
        return pResp;
    }

}

在您的web.xml中,您可以向Servlet添加两个init-params,指向此AccessHandler,如下所示:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>

此课程现在是每个请求的第一个也是最后一个。你可以在这里做任何你想做的事。使用您的记录器记录它,使用来自logback的MDC等.pp。

希望这有帮助!

答案 1 :(得分:0)

您可以实现com.sun.jersey.spi.container.ContainerRequestFilter并将其作为init-param添加到Jersey Servlet。

我在这里使用JerseySpringServlet:

<servlet>
    <servlet-name>Jersey Spring Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
        <param-value>com.domain.my.implements.containerrequestfilter.MyFilter</param-value>
    </init-param>
</servlet>

答案 2 :(得分:0)

如果您使用的是Jersey 2 +

@Provider
public final class GeneralAccessHandler implements ContainerRequestFilter, ContainerResponseFilter {

    @Context
    private HttpServletRequest servReq;

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        System.out.println("Method: " + requestContext.getMethod());
        System.out.println("Path: " + requestContext.getUriInfo().getPath());
        System.out.println("User Agent: " + requestContext.getHeaderString("user-agent"));
        final String addr = servReq.getRemoteAddr();
        final int port = servReq.getRemotePort();
        System.out.println("Client: " + addr + ":" + port);
        System.out.println("URI: " + requestContext.getUriInfo().getRequestUri());
    }

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        //When there is no response
    }
}

按照接受的答案提及注册servlet:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>your.fully.qualified.GeneralAccessHandler</param-value>
</init-param>