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?
Spring MVC already provides a mechanism for distinguishing between json and html, the RequestMapping annotation takes a consumes attribute which looks at the content type of the request...
// REST version, Content-type is "application/json"
@RequestMapping(value = "/", consumes = "application/json")
public void myRestService() {
...
// HTML version, Content-type is not "application/json"
@RequestMapping(value = "/", consumes = "!application/json")
public void myHtmlService() {
...
Another way to use the same url but have distinct methods is with the param or headers attribute...
// the url is /?role=guest
@RequestMapping(value = "/", param = "role=guest")
public void guestService() {
// the url is / with header role=admin
@RequestMapping(value = "/", headers = "role=admin")
public void adminService() {
I would think you would want distinct urls for security. Typically, with something like Spring Security, you would put all of the admin functionality under /admin and let the framework manage it all...
<http auto-config="true">
<intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
...
Would this be sufficient for your use case(s)?
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