We have an application with path pattern like this:
/{language}/{subsystem}/blah/blah
the language
and subsystem
variable will do some common logic and apply to all 100+ controllers, i wanna ask instead of repeating this common logic 100 times in each controller, is there a way to define a centralized controller like
/{language}
which is to handle the language logic and another centralized controller like
and /{subsystem}
which is to handle the subsystem logic and all other controller kinda of 'extend' from these controllers ? Hope i describe it clearly. many thanks and happy weekend:)
You could think about writing a custom Interceptor for your application. This interceptor could fetch the language
and subsystem
parts from your URL path and invoke your common logic in a central place.
There are a few pitfalls to this approach that you should balance carefully:
Personally, I'd take another approach and go for a combination of @RequestMapping
and @PathVariable
annotations on every controller method to capture language
and subsystem
and put the common logic into a helper method:
@GetMapping("/{language}/{subsystem}/something")
public String doSomething(@PathVariable Language language, @PathVariable Subsystem subsystem) {
LanguageSubsystemHelper.doSomething(language, subsystem);
// ...
}
Reduce the repetition to a minimum by providing a helper that's available for all controller methods. The benefits of this approach are:
language
and subsystem
by binding the path variables to an enumIf 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