基于某些业务逻辑,我需要在请求到达控制器之前向请求添加查询参数。为了实现这一点,我使用了HandlerInterceptorAdapter来拦截请求。 我有一个公用的lib,在其中定义了一个拦截器,从属项目具有控制器。 拦截器正在注册,但是当我调用任何api调用时都不会被调用。
通用lib中的拦截器
@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, java.lang.Object handler)
throws ServletException, IOException {
request.setAttribute("id", "123"); // this would be populated based on logic
return true;
}
}
依赖性
@Configuration
public class Resolver implements WebMvcConfigurer {
@Autowired
RequestInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor);
}
}
答案 0 :(得分:0)
您不必注释RequestInterceptor
。请从RequestInterceptor
类的@Autowired
中删除@Component注释,并从Resolver
类中删除此依赖性的public class RequestInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, java.lang.Object handler)
throws ServletException, IOException {
request.setAttribute("id", "123"); // this would be populated based on logic
return true;
}
}
@Configuration
@EnableWebMvc
public class Resolver implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RequestInterceptor());
}
}
注释。
代替注入,请通过默认构造函数实例化它。
...
replicaCount: 1
image:
repository: turfff/node-replicas
tag: latest
pullPolicy: IfNotPresent
...
service:
type: LoadBalancer
port: 80
targetPort: 8080
...