java - Spring Boot GetMapping 的反向 URL 解析
问题描述
我目前正在编写 Spring Boot REST API,并希望对GetMapping
. 在 Django Web 框架中,有一种方法可以做到这一点:reverse。但我无法为 Spring Boot 找到类似的东西。
我正在寻找这样的东西:
package help.me.stack.overflow;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("api")
public class SomeController {
@GetMapping(value = "/items/{filter}/blob")
public ResponseEntity<?> getItems(@PathVariable String filter, @RequestParam String sorting) {
return ResponseEntity.ok().body("here you go");
}
@GetMapping(value = "/give-me-url")
public ResponseEntity<?> getTheUrl() {
return resolveUrl(SomeController.getItems, "foo-filter", "foo-sorting");
// should produce "api/items/foo-filter/blob?sorting=foo-sorting"
}
}
存在这样的东西吗?我该如何使用它?
解决方案
是的,您可以WebMvcLinkBuilder.linkTo(...)
从 Spring HATEOAS 使用:
linkTo(methodOn(SomeController.class).getItems("foo-filter", "foo-sorting")).withSelfRel()
有关更多示例,请参见此处
推荐阅读
- python - 根据形状和颜色比较两个图像
- jenkins - 如何使用 API 列出 Nexus 存储库中的所有组件?
- python - 是否可以在 Fedora - Gnome (gdm) 顶部栏上更改 tkinter 应用程序的标题?
- asp.net-mvc - Asp.net core MVC (Razor Pages) - 在帖子上绑定嵌套的对象列表
- php - 如何在 Laravel 中传递联系表用户电子邮件而不是 SMTP 电子邮件
- swift - AVAudioEngine mainMixerNode pan 不工作
- go - 在 Go 二进制文件中查找最大的导入包
- python - Mask R CNN,准备数据集
- angular - 离子 window.open() 授予权限 demied 并且 window.addeventlistner 没有触发
- python - 如何解析 URL 字符串列表?