I have a Spring mvc (3.1.1) app, and I want to define conditions beyond what's available in RequestMapping. I have a couple of things I want to use it for.
First, it would be nice if I could show a different home page for different user types:
@Controller
public class HomepageController {
@RequestMapping(value = "/")
@CustomCondition(roles = Guest.class)
public String guestHome() { /*...*/ }
@RequestMapping(value = "/")
@CustomCondition(roles = Admin.class)
public String adminHome() { /*...*/ }
}
Second, I want the app to function both as a web site and as a REST service (e.g. for mobile apps), so I'd want to let the website access both html and json actions, and let the service (different subdomain) only access json actions (some kind of @CustomCondition(web = true)
which only matches website urls)
Can this work for any of the two uses I'm planning?
I found very little documentation about custom conditions, but I did find one example that implements custom conditions which might be what I want, but it uses a @Configuration
class instead of the XML configuration which I'm using and I don't want to move my entire spring xml definitions to a @Configuration
class.
Can I define a customMethodCondition for RequestMappingHandlerMapping
in the XML?
I tried subclassing RequestMappingHandlerMapping
and override getCustomMethodCondition
, to return my custom RequestCondition
, but it didn't work - getMatchingCondition()
in my condition didn't fire.
Any help would be greatly appreciated!
UPDATE
I read a little more, and it looks like RequestMappingHandlerMapping
is a new class (since ver 3.1).
What happens in my app is that the @Configuration that tries to override and thereby redefine the requestMappingHandlerMapping
bean actually works, but the url mappings (@RequestMapping
methods in @Controller
s) seem to get processed twice, once by the subclass ExtendedRequestMappingHandlerMapping
and once by the original RequestMappingHandlerMapping
--first with a custom condition, and then again without it.
Bottom line is my custom conditions are simply ignored.
This is supposed to be an advanced pattern, but IMO it should be quite common...
Comments anyone?