如何在Spring REST控制器中拦截所有请求? [英] how to intercept all requests in spring REST controllers?

查看:323
本文介绍了如何在Spring REST控制器中拦截所有请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆控制器,例如:

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/

但它永远不会输入:(

因为我只使用注释,所以我没有XML来定义拦截器, 我发现它的设置如下:

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:

@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?

推荐答案

显然我在做错事,但不能说什么,

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

像这样定义拦截器:

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

我很确定您也可以在纯Java中定义它,但这是可行的,

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

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

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

这篇关于如何在Spring REST控制器中拦截所有请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆