从拦截器中获取发起HTTP调用的方法名称

huangapple go评论90阅读模式
英文:

Retrieving the name of the method from which HTTP call was made using an Interceptor

问题

我正在使用 Spring 的 ClientHttpRequestInterceptor 来捕获我应用程序中的所有外发 HTTP 调用,以便记录数据。除了在拦截器中已经收集的数据之外,我还想以某种方式获取发起 HTTP 调用的函数名称。例如,如果一个名为 getStuffFromUrl 的方法使用 Spring RestTemplate 进行如下的 HTTP 调用,

  1. public String getStuffFromUrl() {
  2. ...
  3. return restTemplate.exchange(url, HttpMethod.GET,entity, String.class).getBody();
  4. }

当我在拦截器中捕获这个外发的 HTTP 调用时,我希望能够同时检索到方法 getStuffFromUrl 的名称。我应该如何实现这个目标呢?

英文:

I am using the Spring ClientHttpRequestInterceptor to capture all outgoing HTTP calls from my applications in order to log the data. In addition to the data that I am already collecting in the interceptor, I want to somehow fetch the name of the function from which the HTTP call originated. So, as an example, if a method called getStuffFromUrl is making the HTTP call using the Spring RestTemplate as follows,

  1. public String getStuffFromUrl() {
  2. ...
  3. return restTemplate.exchange(url, HttpMethod.GET,entity, String.class).getBody();
  4. }

when I capture this outbound HTTP call in my interceptor, I want to retrieve the name of the method getStuffFromUrl as well. How could I go about doing this?

答案1

得分: 1

如果您被允许修改您的HTTP请求,一种方法是添加一个临时的HTTP标头来表示方法名:

  1. public String getStuffFromUrl() {
  2. HttpHeaders headers = new HttpHeaders();
  3. headers.add("JavaMethod", "getStuffFromUrl");
  4. entity = new Entity(headers)
  5. ...
  6. return restTemplate.exchange(url, HttpMethod.GET, entity, String.class).getBody();
  7. }

然后,在实际发送HTTP请求之前,您可以从ClientHttpRequestInterceptor中获取方法名并删除标头:

  1. ClientHttpResponse intercept(HttpRequest request, byte[] body,
  2. ClientHttpRequestExecution execution)
  3. throws IOException {
  4. String javaMethodName = "Unknown";
  5. List<String> javaMethodHeader = request.getHeaders().remove("JavaMethod");
  6. if (javaMethodHeader != null && javaMethodHeader.size() > 0) {
  7. javaMethodName = javaMethodHeader.get(0);
  8. }
  9. log.info("Calling method = " + javaMethodName);
  10. return execution.execute(request, body);
  11. }

(提供的代码未经测试)

英文:

If you are allowed to modify your HTTP request, one way would be to add a ad-hoc HTTP header for the method name :

  1. public String getStuffFromUrl() {
  2. HttpHeaders headers = new HttpHeaders();
  3. headers.add(&quot;JavaMethod&quot;, &quot;getStuffFromUrl&quot;);
  4. entity = new Entity(headers)
  5. ...
  6. return restTemplate.exchange(url, HttpMethod.GET,entity, String.class).getBody();
  7. }

You could then get back the method name and remove the header from within the ClientHttpRequestInterceptor prior the HTTP request is actualy sent out.

  1. ClientHttpResponse intercept(HttpRequest request, byte[] body,
  2. ClientHttpRequestExecution execution)
  3. throws IOException {
  4. String javaMethodName=&quot;Unknown&quot;;
  5. List&lt;String&gt; javaMethodHeader = request.getHeaders().remove(&quot;JavaMethod&quot;);
  6. if(javaMethodHeader!=null &amp;&amp; javaMethodHeader.size()&gt;0) {
  7. javaMethodName = javaMethodHeader.get(0);
  8. }
  9. log.info(&quot;Calling method = &quot;+ javaMethodName);
  10. execution.execute(request, body);
  11. }

(provided code not tested)

huangapple
  • 本文由 发表于 2020年10月6日 14:16:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64220282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定