Looking for a solution to extend Spring MVC with another Component/Annotation

architecture, spring, spring-mvc

Solution

HandlerInterceptor can be used to intercept the RequestMapping handling. This is a simple example how to configure and implement one.

You can check for your session variable and will have a bunch of methods that will allow you to do custom processing or just exchange the view from the normal controller handling with your mobile view.

Problem

Suppose I have a Website that is used in normal mode (browser) and in some other mode, like a MobileView mode (inside a mobile app). For each Controller I create, there might be correspondent controller for MobileView, processing the same url. The easiest solution is to create ifs in all the Controllers that have MobileView logic. Another solution would be to use a correspondent url for MobileView (similar to the normal url) and two separate Controllers (possible where one extends from another; or use some other way to recycle common code) But, a more elegant solution would be to have some extra annotations, like @SupportsMobileView (to mark a controller, and tell the app that this will have a correspondent MobileView Controller) and @MobileViewController (to mark a second controller, and tell the app that this controller needs to run immediately after the initial controller marked with @SupportsMobileView). The link between a normal controller and a MobileView controller would be through the url they process (defined with @RequestMapping). Is it possible to extend Spring MVC (A)? Where to inject new annotation scanners (B) and annotation handlers / component handlers (C)? How should the MobileView controller be executed (D) (right now I am thinking that it could be executed through AOP, where the new handler of my new controller type programatically creates a Join-Point on the corresponding normal controller) Note that I did not mention how this MobileView mode is triggered and detected. Let's just say that there a Session boolean variable (flag) for that. Critics on any points (A), (B), (C) or (D) are welcomed, as well as technical hints and alternative solution to any point or the whole solution.

Original source