spring boot getmapping的反向url解析

irlmq6kh  于 2021-07-22  发布在  Java
关注(0)|答案(1)|浏览(269)

我目前正在编写一个springbootsrestapi,并希望对在 GetMapping . 在djangoweb框架中,有一种方法可以做到这一点:反向。但我找不到类似的春靴。
我在找这样的东西:

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"
    }
}

这样的事存在吗?我怎么用?

qf9go6mv

qf9go6mv1#

是的,你可以用 WebMvcLinkBuilder.linkTo(...) 来自spring hateoas:

linkTo(methodOn(SomeController.class).getItems("foo-filter", "foo-sorting")).withSelfRel()

更多示例请参见此处

相关问题