[英]Reverse URL resolution for Spring Boot GetMapping
I am currently writing a Spring Boot REST API and wanted to do a reverse resolution of a URL that is defined in a 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"
}
}
存在这样的东西吗? 我该如何使用它?
是的,您可以使用 Spring HATEOAS 中的WebMvcLinkBuilder.linkTo(...)
:
linkTo(methodOn(SomeController.class).getItems("foo-filter", "foo-sorting")).withSelfRel()
有关更多示例,请参见此处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.