java Spring MVC 请求映射,这可以是动态/可配置的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12710307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC Request mapping, can this be dynamic/configurable?
提问by loyalflow
With Spring MVC, I know how you set the RequestMapping
in every controller and method/action.
使用 Spring MVC,我知道您如何RequestMapping
在每个控制器和方法/操作中设置。
But what if I wanted this to be configurable, so for example I the following controllers:
但是如果我希望它是可配置的,例如我使用以下控制器:
BlogController
- with methods for listing blogs entries, single entry, new, update, etc.
ArticleController
- with methods for listing articles entries, single entry, new, update, etc.
Now in my application, the administrator can setup 2 blogs for the webiste, and 1 article section so the urls would be like:
现在在我的应用程序中,管理员可以为网站设置 2 个博客和 1 个文章部分,因此网址如下:
www.example.com/article_section1/ - uses ArticleController
www.example.com/blog1/ - uses BlogController
www.example.com/blog2/ - uses BlogController
Maybe after a while the administrator wants another article section, so they just configure that with a new section like:
也许过了一会儿,管理员想要另一个文章部分,所以他们只是用一个新的部分来配置它,比如:
www.example.com/article_section2/
This has to work dynamically/on-the-fly without having to restart the application of course.
这必须动态/即时工作,而无需重新启动应用程序。
My question is only concerned with how I will handle url mappings to my controllers.
我的问题只与我将如何处理到我的控制器的 url 映射有关。
How would this be possible with Spring MVC?
Spring MVC怎么可能做到这一点?
I only know how to map urls to controllers using @RequestMapping("/helloWorld")
at the controller or method level, but this makes the url mappings fixed and not configurable like how I want it.
我只知道如何@RequestMapping("/helloWorld")
在控制器或方法级别使用将 url 映射到控制器,但这使得 url 映射固定且无法像我想要的那样进行配置。
Update:
更新:
I will be storing the paths in the database, and with the mapping to the type of controller so like:
我将在数据库中存储路径,并映射到控制器的类型,例如:
path controller
/article_section1/ article
/blog1/ blog
/blog2/ blog
..
With the above information, how could I dispatch the request to the correct controller?
有了上述信息,我如何将请求分派给正确的控制器?
Again, not looking to reload/redeploy, and I realize this will require more work but its in the spec :)
同样,不打算重新加载/重新部署,我意识到这将需要更多的工作,但它在规范中:)
回答by parsifal
Would this sort of URL mapping work for you?
这种 URL 映射对您有用吗?
www.example.com/blog/1/
www.example.com/blog/2/
If yes, then that's easy: Spring 3 supports path variables: http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestmapping-advanced
如果是,那么这很简单:Spring 3 支持路径变量:http: //static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestmapping-advanced
Alternatively, you can create a generic request mapping and your own sub-dispatcher that reads a config file, but I think that's probably more work than it's worth.
或者,您可以创建一个通用请求映射和您自己的读取配置文件的子调度程序,但我认为这可能比它的价值更多。
回答by Rasmus Franke
Truly changing the request mappings at runtime might be hard (and not really recommended, since small errors can easily occur). If you still wish to do it, perhaps JRebel, and more specificly LiveRebel can be interesting for live redeployment of code and configuration.
在运行时真正改变请求映射可能很困难(并不真正推荐,因为很容易发生小错误)。如果您仍然希望这样做,也许 JRebel,更具体地说 LiveRebel 可能对代码和配置的实时重新部署感兴趣。
Otherwise, like other posts suggested, RequestMappings supports wildcards, the limits of this should be clear after a quick read of the official documentation.
否则,就像其他帖子建议的那样,RequestMappings 支持通配符,在快速阅读官方文档后应该会清楚它的限制。
回答by Jayaveera
Try using with @RequestMapping wild cards as below:
尝试使用 @RequestMapping 通配符,如下所示:
@RequestMapping(value="/article_section*/"}
public void getArticle(....){
//TODO implementation
}
@RequestMapping(value="/blog*/"}
public void getBlog(....){
//TODO implementation
}
Hope this helps!!!
希望这可以帮助!!!
回答by Jo?o Dias Amaro
Also another solution might be to create a custom annotation that holds the already defined path on the @RequestMapping
and also the new one to apply, let's say @ApiRestController
.
另一种解决方案可能是创建一个自定义注释,其中包含已定义的路径@RequestMapping
以及要应用的新路径,例如@ApiRestController
.
Then, before the Spring context loads, the @Controller
classes can be changed to have their annotation values changed at runtime by the new one (with the desired path). By doing this, Spring will load the enhanced request mapping and not the default one.
然后,在 Spring 上下文加载之前,@Controller
可以更改类以在运行时通过新的(具有所需路径)更改它们的注释值。通过这样做,Spring 将加载增强的请求映射而不是默认映射。
Created a small project to exemplify this for someone that needs this in the future https://gitlab.com/jdiasamaro/spring-api-rest-controllers.
创建了一个小项目,为将来需要此功能的人提供示例https://gitlab.com/jdiasamaro/spring-api-rest-controllers。
Hope it helps. Cheers.
希望能帮助到你。干杯。
回答by sgpalit
doesn't this work? @RequestMapping("/helloWorld*")
这行不通?@RequestMapping("/helloWorld*")