Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to intercept all requests in spring REST controllers?

Tags:

java

rest

spring

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?

like image 792
Alfredo M Avatar asked Feb 05 '16 20:02

Alfredo M


People also ask

Which intercepts all requests in spring MVC application?

Spring Interceptor are used to intercept client requests and process them.

How do I intercept incoming requests in spring boot?

Spring-boot allows us to configure custom interceptors. Usually in a spring boot application everything is auto configured and in such cases we can customize it by using the WebMvcConfigurerAdapter . Just extend WebMvcConfigurerAdapter and provide the configurations that you need in this class.

How do you make a spring interceptor for spring RESTful web services?

@Configuration public class AppConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry. addInterceptor(new HttpInterceptor()); // registry. addInterceptor(new HttpInterceptor()). addPathPatterns("/account/login"); you can add specific end point as well. } }


2 Answers

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 {
// ...
}
like image 182
Arjit Avatar answered Sep 28 '22 20:09

Arjit


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/

like image 37
Alfredo M Avatar answered Sep 28 '22 21:09

Alfredo M