如何将@PathVariable注入字段?

时间:2017-11-24 02:29:44

标签: spring spring-mvc dependency-injection

假设我有跟随控制器。

@RestController()
@RequestMapping("/my/{path}")
public class MyController {

    public void some1(@PathVariable("path") String path) {
    }

    public void some2(@PathVariable("path") String path) {
    }

    public void some3(@PathVariable("path") String path) {
    }
}

现在我想将path注入一个字段。

// I usually do this with JAX-RS 
@RequestScope // I added this!
@RestController()
@RequestMapping("/my/{path}")
public class MyController {

    public void some1() {
    }

    public void some2() {
    }

    public void some3() {
    }

    // single declaration for all methods
    // I know ElementType.FIELD is not one of @PathVariable's target
    // Is there any equivalent way to do this with Spring?
    @PathVariable("path")
    String path
}

不编译。

我该怎么做?

2 个答案:

答案 0 :(得分:0)

请求网址:/ a / b / c

private String some;

@RequestMapping(value = "/a/{some}/c")
public void some(@PathVariable("some") String some) {
  this.some = some;
}

答案 1 :(得分:-1)

@PathVariable Annotation,指示方法参数应绑定到URI模板变量。

示例:



<dd ng-bind-html="vm.model.PERSON_INFO | ageFilter"></dd>
&#13;
&#13;
&#13;

请参阅以下链接:

Spring mvc @PathVariable

https://www.journaldev.com/3358/spring-requestmapping-requestparam-pathvariable-example