泽西岛客户端:记录HTTP重定向

时间:2020-03-03 10:47:30

标签: java rest jersey jax-rs jersey-client

我正在将JAX-RS与Jersey配合使用。向API发送POST请求时,我收到302,Jersey会自动跟随,结果为403。但是,在我的日志记录中,我只能看到失败的响应:

INFO Rest-Request:  POST http://[Jenkins-IP]/job/RestTestingArea/job/testJob/doDelete
INFO Rest-Response: POST 403 http://[Jenkins-IP]/job/RestTestingArea/job/testJob/doDelete

我确定在请求和响应之间存在重定向,因为当我关闭重定向时,日志记录输出将发生如下变化:

INFO Rest-Request:  POST http://[Jenkins-IP]/job/RestTestingArea/job/testJob/doDelete
INFO Rest-Response: POST 302 http://[Jenkins-IP]/job/RestTestingArea/testJob/doDelete

除了完全关闭重定向并自己完成第二步之外,是否有任何方法可以实际记录这些重定向?记录:

public class LoggingFeature implements ClientRequestFilter, ClientResponseFilter
{
  private final Logger logger;

  public LoggingFeature(Logger log)
  {
    this.logger = log;
  }

  @Override
  public void filter(ClientRequestContext requestContext) throws IOException
  {
    try
    {
      logger.info(String.format("Rest-Request:  %s %s", requestContext
          .getMethod(), requestContext.getUri()));
    }
    catch (Exception ex)
    {
      logger.warn("LoggingFeature: ", ex);
    }
  }

  @Override
  public void filter(ClientRequestContext requestContext,
      ClientResponseContext responseContext) throws IOException
  {
    try
    {
      logger.info(String.format("Rest-Response: %s %d %s", requestContext
          .getMethod(), responseContext.getStatus(), requestContext.getUri()));
    }
    catch (Exception ex)
    {
      logger.warn("LoggingFeature: ", ex);
    }
  }
}

请求:

    HttpAuthenticationFeature auth = HttpAuthenticationFeature.basicBuilder()
        .credentials(username, token)
        .build();
    Client client = ClientBuilder.newBuilder()
        .register(auth)
        .register(new LoggingFeature(LOG))
        .build();
    WebTarget deleteTarget = client.target("http://[Jenkins-IP]/job/RestTestingArea/job/testJob/doDelete").    
    Response response = deleteTarget.request()
        .post(null);

0 个答案:

没有答案