RequestMappingHandlerMapping

RequestMappingHandlerMapping

本文介绍了在 Spring mvc 3.1 中添加自定义 RequestCondition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Spring mvc (3.1.1) 应用程序,我想定义 RequestMapping 中可用的条件之外的条件.我有几件事想用它来做.

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() { /*...*/ }

}

其次,我希望该应用程序既可以用作网站又可以用作 REST 服务(例如对于移动应用程序),所以我想让该网站同时访问 html 和 json 操作,并让该服务(不同的子域)只访问 json 操作(某种 @CustomCondition(web = true) 只匹配网站 url)

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?

我发现关于自定义条件的文档很少,但我确实找到了一个例子实现自定义条件 这可能是我想要的,但它使用 @Configuration 类而不是我正在使用的 XML 配置,我不想移动我的整个 spring xml 定义到 @Configuration 类.

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.

我可以在 XML 中为 RequestMappingHandlerMapping 定义一个 customMethodCondition 吗?

Can I define a customMethodCondition for RequestMappingHandlerMapping in the XML?

我尝试子类化 RequestMappingHandlerMapping 并覆盖 getCustomMethodCondition,以返回我的自定义 RequestCondition,但它没有用 - getMatchingCondition() 在我的情况下没有触发.

I tried subclassing RequestMappingHandlerMapping and override getCustomMethodCondition, to return my custom RequestCondition, but it didn't work - getMatchingCondition() in my condition didn't fire.

任何帮助将不胜感激!

更新

我读了一点,看起来 RequestMappingHandlerMapping 是一个新类(自 3.1 版起).

I read a little more, and it looks like RequestMappingHandlerMapping is a new class (since ver 3.1).

在我的应用程序中发生的事情是,试图覆盖并重新定义 requestMappingHandlerMapping bean 的 @Configuration 实际上有效,但 url 映射(@RequestMapping@RequestMappingcode>@Controllers) 似乎被处理两次,一次由子类 ExtendedRequestMappingHandlerMapping 处理,一次由原始 RequestMappingHandlerMapping --首先使用自定义条件,然后再不使用它.

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 @Controllers) 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.

这应该是一种高级模式,但 IMO 应该很常见...

This is supposed to be an advanced pattern, but IMO it should be quite common...

评论任何人?

推荐答案

Spring MVC 已经提供了区分 json 和 html 的机制,RequestMapping 注解采用了一个 consumes 属性来查看请求的内容类型...

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() {
...

另一种使用相同 url 但具有不同方法的方法是使用 param 或 headers 属性...

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() {

为了安全起见,我认为您会需要不同的网址.通常,使用 Spring Security 之类的东西,您会将所有管理功能放在/admin 下,并让框架管理所有这些...

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)?

这篇关于在 Spring mvc 3.1 中添加自定义 RequestCondition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:37