SpringMVC - 调度程序servler的url模式样式

Eri*_*ang 9 java rest servlets spring-mvc

我正在尝试使用构建一个宁静的样式API springMVC.

配置springMVC的url-pattern时DispatcherServlet,似乎有两个选择,我需要一些建议.

选择A:
配置模式为:<url-pattern>*.action</url-pattern>
和动作使用路径之类的@RequestMapping("/role/add.action")

选择B:
配置模式为:<url-pattern>/api/*</url-pattern>
和动作使用路径之类的@RequestMapping("/api/role/add")

我更喜欢使用没有后缀的样式,但在这种情况下我需要添加一个子路径.

但我不确定哪个更适合用作提供restful API的后端项目,以浏览器/ IOS/Android作为其客户端.


可能有选择C,但我不确定:

config pattern as:<url-pattern>/*</url-pattern>
和action使用路径之类的@RequestMapping("/role/add")

在这种情况下,内置servlet将被覆盖,例如jsp将无法正常工作.
但是我没有任何jsp静态资源,例如html / js / css / image / document / music / video全部放在另一个端口或服务器上nginx,请求tomcat只通过json数据提供ajax服务.
那么在这种情况下使用选择C是否合适,或者它有一些不良的副作用?

Gio*_*nni 3

如果你的目标是restful api,我的选择是第二个,因为你在url中识别了资源;假设您必须管理一个角色资源,您应该有一些像这样的映射:

@RequestMapping("/api/role" method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)

插入一个新角色(可能是 api 不允许这样做)

@RequestMapping("/api/role/{roleId}" method = RequestMethod.PUT)
Run Code Online (Sandbox Code Playgroud)

更新现有角色

@RequestMapping("/api/role/{roleId}" method = RequestMethod.DELETE)
Run Code Online (Sandbox Code Playgroud)

删除角色

@RequestMapping("/api/role" method = RequestMethod.GET)
Run Code Online (Sandbox Code Playgroud)

检索角色(您可以通过查询字符串实现一些过滤器)

这同样适用于其他资源(用户等),命名模式是相同的。

我会避免选择 C,因为我认为如果您的应用程序还提供了一个不使用 api 的 Web 界面,那么最好为 api 有一个专用的映射