如何在 spring-boot-web 中具有 class 级别 @RequestMapping 的方法上添加字符 's'
How to add character 's' on method that have class level @RequestMapping in spring-boot-web
获得class/interface级别和方法映射
@RequestMapping(value = "/post")
public interface PostApi {
//to get /posts
@RequestMapping(value = "s")
ResponseEntity getAll();
}
基本上我想在 /post 上添加字符 's' 并得到 /posts,这怎么可能
您不能更改上下文路径,而是删除“/post”,然后在不同的任务中使用下面的“/post”和“/posts”。
public interface PostApi {
//to get /posts
@RequestMapping(value = "/posts")
ResponseEntity getAll();
@GetMapping(value = "/post/{id}")
ResponseEntity getById(@PathParam Long id);
}
您不能将路径值连接成单个路径字符串,因为它们在 URI 规范中被视为对象,而不是多个字符串。
(如果您喜欢 RFC,请查看:https://datatracker.ietf.org/doc/html/rfc3986#section-3.3)
所以基本上你的 class-level Mapping 已经通过设计设置了层次结构,并且该方法只能配置该层次结构内的较低级别
((较早的)spring 文档对此进行了说明:
“方法级映射只允许缩小 class 级(如果有)表达的映射。”
解决此问题的唯一方法实际上是删除 class 映射并将单独的映射添加到任一方法(如其他人所述)。
作为副作用:这使您的代码更具可读性,因此审阅者(或 3 个月后的您自己)在尝试理解控制器时不必在脑海中连接完整路径
获得class/interface级别和方法映射
@RequestMapping(value = "/post")
public interface PostApi {
//to get /posts
@RequestMapping(value = "s")
ResponseEntity getAll();
}
基本上我想在 /post 上添加字符 's' 并得到 /posts,这怎么可能
您不能更改上下文路径,而是删除“/post”,然后在不同的任务中使用下面的“/post”和“/posts”。
public interface PostApi {
//to get /posts
@RequestMapping(value = "/posts")
ResponseEntity getAll();
@GetMapping(value = "/post/{id}")
ResponseEntity getById(@PathParam Long id);
}
您不能将路径值连接成单个路径字符串,因为它们在 URI 规范中被视为对象,而不是多个字符串。 (如果您喜欢 RFC,请查看:https://datatracker.ietf.org/doc/html/rfc3986#section-3.3)
所以基本上你的 class-level Mapping 已经通过设计设置了层次结构,并且该方法只能配置该层次结构内的较低级别 ((较早的)spring 文档对此进行了说明: “方法级映射只允许缩小 class 级(如果有)表达的映射。”
解决此问题的唯一方法实际上是删除 class 映射并将单独的映射添加到任一方法(如其他人所述)。 作为副作用:这使您的代码更具可读性,因此审阅者(或 3 个月后的您自己)在尝试理解控制器时不必在脑海中连接完整路径