jersey.api.client.WebResource - 如何调试/记录请求标头

时间:2011-08-22 15:09:43

标签: http-headers jersey

我正在使用jersey生成http请求,我希望能够在发送之前看到请求(出于调试目的)。

例如:

WebResource resource = client.resource(url);
resource.header("aa", "bb");
resource.getHeaders(); // Error: how can I do it?

感谢

2 个答案:

答案 0 :(得分:9)

您可以使用LoggingFilter,如图所示here

答案 1 :(得分:-2)

您需要添加init-params,然后实现ContainerRequestFilter

将其放入web.xml请注意,com.az.jersey.server.AuthFilter是您已经实施上述接口的课程。

<init-param>
            <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
            <param-value>com.sun.jersey.api.container.filter.LoggingFilter;com.az.jersey.server.AuthFilter</param-value>
        </init-param>



public class AuthFilter implements ContainerRequestFilter {
    /**
     * Apply the filter : check input request, validate or not with user auth
     * 
     * @param containerRequest
     *            The request from Tomcat server
     */
    @Override
    public ContainerRequest filter(ContainerRequest containerRequest) throws WebApplicationException {
        //GET, POST, PUT, DELETE, ...
        String method = containerRequest.getMethod();
        // myresource/get/56bCA for example
        String path = containerRequest.getPath(true);         

        return containerRequest;
    }