我正在尝试记录第三方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();
}
}
最佳答案
注册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以获取更多提示关于java - 获取路径在Feign中可变,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56831241/