jersey.api.client.WebResource - how to debug/log the request headers

无人久伴 提交于 2019-12-07 04:38:43

问题


I am using jersey to generate http requests and I would like to be able to see the request before it is sent (for debugging purposes).

For example:

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

thanks


回答1:


You can use LoggingFilter, as shown here




回答2:


You need to add init-params and then implement ContainerRequestFilter

Put it in your web.xml Please note that com.az.jersey.server.AuthFilter is your class that has implemented mentioned above interface.

<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;
    }


来源:https://stackoverflow.com/questions/7149548/jersey-api-client-webresource-how-to-debug-log-the-request-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!