【问题标题】:Getting the pathVariable in Feign在 Feign 中获取 pathVariable
【发布时间】:2019-07-01 07:01:41
【问题描述】:

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

这是我的示例 feign 客户端方法:

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

这是我在 feign 中截获响应并能够记录 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();
 }
}

【问题讨论】:

  • @RequestMapping(value = "/send/{name}", method = RequestMethod.GET) SendEmailRemoteResponse hello(@PathVariable("name") List name);
  • 你好,这是什么? @阿拉夫
  • 请在@GetMapping注解中添加/{name}。

标签: java spring spring-cloud-feign feign


【解决方案1】:

注册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 了解更多提示

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    相关资源
    最近更新 更多