java 如何拦截spring REST控制器中的所有请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35232597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:52:44  来源:igfitidea点击:

how to intercept all requests in spring REST controllers?

javaspringrest

提问by Alfredo M

I have a bunch of controllers like:

我有一堆控制器,例如:

@RestController
public class AreaController {
    @RequestMapping(value = "/area", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<Area> get(@RequestParam(value = "id", required = true) Serializable id) { ... }
}

and I need to intercept all the requests that reach them,

我需要拦截所有到达他们的请求,

I created an interceptor like this example:

我创建了一个像这个例子的拦截器:

http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/

http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/

but it never enters :(

但它永远不会进入:(

because I'm using only annotations, i don't have a XML to define the interceptor, what I've found its to set it like this:

因为我只使用注释,所以我没有一个 XML 来定义拦截器,我发现它是这样设置的:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.test.app")
public class AppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ControllerInterceptor getControllerInterceptor() {
        ControllerInterceptor c = new ControllerInterceptor();
        return c;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getControllerInterceptor());
        super.addInterceptors(registry);
    }

}

what am i doing wrong or am i missing something?

我做错了什么还是我错过了什么?

采纳答案by Alfredo M

so apparently i was doing something wrong but can't say what,

所以显然我做错了什么,但不能说是什么,

defining the interceptor like:

定义拦截器,如:

<mvc:interceptors>
  <bean class="com.test.ControllerInterceptor" />
</mvc:interceptors> 

I'm pretty sure that you can also define it in pure java, but this is working,

我很确定你也可以在纯 Java 中定义它,但这是有效的,

answer found in: http://viralpatel.net/blogs/spring-mvc-interceptor-example/

答案在:http: //viralpatel.net/blogs/spring-mvc-interceptor-example/

回答by Arjit

your Interceptorclass ControllerInterceptorisn't an application context managed bean. Make sure you put @Componentannotation on ControllerInterceptor and add it's package to @ComponentScan. So, let's say your ControllerInterceptorresides in package com.xyz.interceptors like:

您的InterceptorControllerInterceptor不是应用程序上下文管理的 bean。确保@Component在 ControllerInterceptor 上添加注释并将它的包添加到@ComponentScan. 因此,假设您ControllerInterceptor驻留在包 com.xyz.interceptors 中,例如:

package com.xyz.interceptors;  //this is your package

@Component //put this annotation here
public class ControllerInterceptor extends HandlerInterceptorAdapter{
 // code here
}

and your AppConfig becomes:

并且您的 AppConfig 变为:

@ComponentScan(basePackages = { "com.test.app", "com.xyz.interceptors" })
public class AppConfig extends WebMvcConfigurerAdapter {
// ...
}

回答by PawelN

Possible you are missing mapping

您可能缺少映射

registry.addInterceptor(getControllerInterceptor()).addPathPatterns("/**");

And as I know you don't have to use

据我所知,您不必使用

super.addInterceptors(registry);