跳转至

@PathVariable

取得url路徑的值

用法: 只能加在方法的參數上

用途: 取得url路徑的值

可以注意到 test 後面並不是接著? 那要怎麼取得123 這個值呢

ex: http://localhost:8080/test/123

練習

@RestController
public class MyController4 {
    @RequestMapping("/test/{id}")
    public String test3(@PathVariable Integer id) {
        System.out.println("path id: " + id);
        return "hello world";
    }
}

Talend API Tester

GET http://localhost:8080/test/123

console:

path id: 123
@RestController
public class MyController4 {
    @RequestMapping("/test/{userid}") // 可彈性修改 ,但要跟下面對應
    public String test3(@PathVariable Integer userid) {
    }
}

Talend API Tester 測試2 多個參數傳遞

GET http://localhost:8080/test/36/ts

@RestController
public class MyController4 {
    @RequestMapping("/test/{id}/{name}")
    public String test3(@PathVariable Integer id ,
                        @PathVariable String name) {
        System.out.println("path id: "   + id);
        System.out.println("path name: " + name);
        return "hello world";
    }
}

console:

path id: 36
path name: ts