Springboot ClientHttpRequestInterceptor:获取请求对象类型

问题描述 投票:0回答:1

我正在一个springboot项目中,我需要拦截传出的http请求并对其进行处理。我知道我们可以使用ClientHttpRequestInterceptor来实现同样的效果,并且效果也不错。

但是我需要知道在HTTP POST / PUT请求中发送的请求正文的java类类型。当前,此拦截器仅提供请求对象的字节数组表示形式。 Doc here

有什么方法可以了解请求对象/主体的java类类型?还是除了使用ClientHttpRequestInterceptor之外,还有其他更好的方法吗?

更新的代码示例:

public class MyInterceptor implements ClientHttpRequestInterceptor {


@Override
public ClientHttpResponse intercept(HttpRequest request,
                             byte[] body,
                             ClientHttpRequestExecution execution)
                      throws IOException {

// Do something before service invocation

// I need to get request body's class type but it is just a byte[] here. And the 'HttpRequest' argument doesn't carry any info related to request class type.

ClientHttpResponse clientHttpResponse = execution.execute(request, body);

// Do something after service invocation    
  }
}
spring spring-boot interceptor clienthttprequest
1个回答
-1
投票

当然可以,推荐的方法是使用ِ面向方面的编程AOP,它将为您提供有关目标关节的所有详细信息;在您的情况下,它将是在控制器内部调用的方法。

检查此示例Spring Boot AOP,最好使用这些类ProceedingJoinPointJointPoint]弄脏您的手”

另一种可能对您的代码有帮助的方法是使用反射和自定义批注,这样,您还可以从您所请求的Class中获取所有详细信息,但可能需要更多代码。

© www.soinside.com 2019 - 2024. All rights reserved.