how to intercept all requests in spring REST controllers?

23,543

Solution 1

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,

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

Solution 2

your Interceptor class ControllerInterceptor isn't an application context managed bean. Make sure you put @Component annotation on ControllerInterceptor and add it's package to @ComponentScan. So, let's say your ControllerInterceptor resides in package com.xyz.interceptors like:

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

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

and your AppConfig becomes:

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

Solution 3

Possible you are missing mapping

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

And as I know you don't have to use

super.addInterceptors(registry);
Share:
23,543

Related videos on Youtube

Alfredo M
Author by

Alfredo M

SCJP SCWCD OCA MCTS ISC melomano  fan Soy una criatura nocturna de clima frio nacido en época de lluvia y q ama en secreto a la luna Minecraft junkie

Updated on January 12, 2020

Comments

  • Alfredo M
    Alfredo M over 4 years

    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/

    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:

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

  • PawelN
    PawelN about 8 years
    Is this spring boot or just plain Spring MVC app? And if you can share code of your interceptor.
  • Alfredo M
    Alfredo M about 8 years
    at the beginning it was springBoot, but we come across some problems and now is plain Spring
  • PawelN
    PawelN about 8 years
    If this is not spring boot so are you sure your AppConfig has been registered in your AnnotationConfigWebApplicationContext at startup?
  • Alfredo M
    Alfredo M about 8 years
    yes it is loaded: <bean id="appConfig" class="com.test.AppConfig"></bean>