SpringMVC - 调度程序servler的url模式样式 [英] SpringMVC - dispatcher servler's url pattern style
问题描述
我正在尝试使用 springMVC
构建一个restful样式API。
I am trying to build a restful style API, using springMVC
.
配置url-pattern时对于springMVC的 DispatcherServlet
,似乎有两个选择,我需要一些建议。
When config the url-pattern for springMVC's DispatcherServlet
, there seems to have 2 choice, and I need some advice.
选择A :
配置模式为:< url-pattern> *。action< / url-pattern>
和动作使用路径如 @RequestMapping(/ role / add.action)
选择B:
配置模式为:< url-pattern> / api / *< / url-pattern>
和行动使用路径如 @RequestMapping(/ api / role / add)
我更喜欢使用没有后缀的样式,但在这种情况下我需要添加子路径。
I prefer to use a style that has no suffix, but in that case I need do add a sub path.
但我不确定在一个项目中使用哪个更适合作为后端提供RESTful API,以浏览器/ IOS / Android作为其客户端。
But I am not sure which is more proper to use in a project that serve as a backend to provide restful API, with browser / IOS / Android as its client.
可能有选择C ,但我不确定:
There might be a choice C, but I am not sure:
配置模式为:< url-pattern> / *< / url-pattern>
和行动使用路径如 @RequestMapping(/ role / add)
在这种情况下内置的servlet将被覆盖,例如jsp将无法正常工作。
但我没有任何 jsp
,而且,静态像 html / js / css / image / document / music / video
这样的资源全部放在由 nginx $ c $提供服务的另一个端口或服务器上c>,请求tomcat只通过json数据提供ajax服务。
所以在这种情况下使用选择C是否正确,或者它有一些不良的副作用?
In this case built-in servlet will be override, e.g jsp won't work normally.
But I don't have any jsp
, and also, static resource like html / js / css / image / document / music / video
are all put on another port or server served by nginx
, request to tomcat only provide ajax service via json data.
So in this case is it proper to use choice C, or it has some bad side-effects?
推荐答案
如果你的目标是宁静的,我的选择就是s第二个,因为你在网址中识别资源;假设你必须管理一个角色资源,你应该有一些这样的映射:
if your goal is restful api my choice is the second one since you identify the resource in the url; say you must manage a role resource you should have some mapping like these ones:
@RequestMapping("/api/role" method = RequestMethod.POST)
插入一个新角色(可能是api不允许这样做)
to insert a new role (may be the api does not allow this)
@RequestMapping("/api/role/{roleId}" method = RequestMethod.PUT)
更新现有角色
@RequestMapping("/api/role/{roleId}" method = RequestMethod.DELETE)
要删除角色
@RequestMapping("/api/role" method = RequestMethod.GET)
检索角色(你可以通过查询字符串实现一些过滤器)
to retrieve roles (you may implement some filters via query string)
相同适用于其他资源(用户等),命名方案是相同的。
The same applies for other resources (User, etc) the naming schema is the same.
我可以避免使用选项C,因为我认为最好有api的专用映射。你的应用程序还提供了一个不是我们的网络界面e api
I vould avoid option C since I think it's best to have a dedicated mapping for the api if you app also ship a web interface that does not use the api
这篇关于SpringMVC - 调度程序servler的url模式样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!