java - Spring中如何使用HttpRequestInterceptor?

标签 java spring http interceptor

我想获取请求正文,所以我决定实现 HttpResponseInterceptor 接口(interface)。如何正确地做到这一点?

public class CustomHttpInterceptor implements org.apache.http.HttpRequestInterceptorHttpRequestInterceptor {

    @Override
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {

    }
}

我应该将 CustomHttpInterceptor 创建为 bean 吗?如何添加它对 Spring 也可见?

最佳答案

您还可以使用spring提供的HandlerInterceptorAdapter实现接口(interface)并添加一个实现WebMvcConfigurer的Config类,该类有一个方法

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new LoggerInterceptor());
}

您可以在 https://www.baeldung.com/spring-mvc-handlerinterceptor 上找到更多详细信息

关于java - Spring中如何使用HttpRequestInterceptor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60186791/

相关文章:

Java 程序实现 boolean 而不是 not (!)

java - 为什么 Java Streams min 方法不接受空值,即使使用的比较器是空友好的?

java - Axis2 不返回自己的对象

java - 通过 gradle 构建后无法找到或加载主类

java - 通过 servlet 处理 XMPP 服务器的登录

php - 通用 REST 前端 - 它存在吗?

google-app-engine - 从 Google App Engine 提供多个端口

java - 添加行后对 JTable 进行排序

jquery - 使用 AJAX、Spring MVC、MySQL 更新表

java - Spring计划任务