I am currently writing some kind of Framework that would allow others to write REST Controllers for it. Naturally, I want those "others" to have as little interaction with whats happening in my code as possible.
Specifically, I want and need to access the requests data (i.e.
RequestEntity before the request is handled by the rest controller. Sort of "intercepting" a request before it is handled by the controller, and only then letting the controller handle it.
Consider the following code:
@RestController
@RequestMapping("/")
public class MyController {
@GetMapping("/")
@ResponseBody
public ResponseEntity<String> getSomething(RequestEntity requestEntity) {
MyClass.doStuffWithRequestEntity(requestEntity);
// ...
Now what I would need is that ExternalClass.doStuffWithRequestEntity(requestEntity); is called without the need to explicitly call it. Is it possible to have some method in some class being called (with the RequestEntity passed to it!) without having to call it explicitly?
Furthermore, said Interceptor class should create and configure an object that is then again made available to the rest controller.
I'd be thinking something like
class RestController {
@RestController
@RequestMapping("/")
public class MyController {
@GetMapping("/")
@ResponseBody
public ResponseEntity<String> getSomething() {
MyClass x = MyClass.getInstanceCreatedByInterceptor();
}
}
}
and
class Interceptor {
public void doStuffWithRequestEntity(requestEntity) {
MyClass x = new MyClass();
x.fillObjectWithData();
}
}
being executed before.
The idea is that EVERY (!) incoming request is parsed and its contents get decoded without the programmer of the rest controller getting having to care about this at all. They should just access data via/from the MyClass instance.
Is there a way to do this?
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.
Remember to add @Configuration annotation so that this class will be picked up by spring during component scan.
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
HandlerInterceptor customInjectedInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(...)
...
registry.addInterceptor(customInjectedInterceptor).addPathPatterns("/**");
}
}
This is how you normally add interceptors to spring boot applications.Hope this might help answer your question.
From spring 5.x.x or spring-boot 2 onwards , WebMvcConfigurerAdapter is marked as deprecated.The WebMvcConfigurer interface (which is implemented by the abstract class WebMvcConfigurerAdapter), starting with Spring 5, contains default implementations for all its methods. As a result, the abstract adapter class was marked as deprecated.You can adopt it if you like as follows :
@Configuration
public WebConfig implements WebMvcConfigurer {
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With