2011-08-22 37 views
5

我使用球衣來生成http請求,我希望能夠在發送(出於調試目的)之前看到請求jersey.api.client.WebResource - 如何調試/記錄請求標頭

例如:

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

感謝

+0

可能重複的[澤西:打印實際的請求(http://stackoverflow.com/questions/6860661/jersey-print-the-actual-request) – mateuscb

回答

9

您可以使用LoggingFilter,如圖here

+0

這是在客戶端。如何在服務器端完成? –

-2

您需要添加的init-PARAMS,然後實現ContainerRequestFilter

把它放在你web.xml請注意,com.az.jersey.server.AuthFilter是你的班級實施了提名d以上界面。的

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