获取路径在Feign中可变

时间:2019-07-01 07:01:41

标签: java spring spring-cloud-feign feign

我正在尝试记录第三方API请求的详细信息,并且能够记录URL,请求和响应正文。

这是我的伪装客户示例方法:

@GetMapping(value = "/{name}")
Customer getDetails(@PathVariable(name = "name") List<String> name);

这是我假装拦截响应并能够记录url,请求和响应的方法。

public class FeignResponseDelegate extends Default {

@Override 
public Response execute(Request request, Request.Options options) throws IOException {
    Response response = super.execute(request, options);


    Request.Body requestBody = request.requestBody();
    InputStream responseBodyInputStream = response.body().asInputStream();
    byte[] bytes = IOUtils.toByteArray(responseBodyInputStream);


    logger.debug("Request URL {}", request.url());
    logger.debug("Request Request body {}", requestBody.length() > 0 ? requestBody.asString() : null);
    logger.debug("Response Body {}", IOUtils.toString(responseBodyInputStream));

    // logger.debug("Path Variables: {}".....
    // how to log the path variables?


    return response.toBuilder()
            .body(bytes)
            .build();
 }
}

1 个答案:

答案 0 :(得分:0)

注册feign.Logger实例将为您解决这个问题。

@Bean
public Logger logger() {
    /* here are the two built in loggers */
    return new JavaLogger(); // for java.util.logging

    /* may require feign-slf4j */
    return new Slf4jLogger(); // if you want to use slf4j
}

Logger将为您记录请求和响应,包括解析的路径参数和标头。看看Feign上的SLF4J Documentation了解更多技巧