@PathVariable是从url地址中取值, 而@RequestParm是从键值对的参数中取值, 都可以为变量名起别名
localhost:8999/callback/auth
localhost:8999/callback/45646/body/46546
localhost:8999/callback?auth_code=4546
localhost:8999/callback/v2?authCode=4546
注意事项:
不能为多个get请求赋同一个请求地址,不同rest风格的请求可以,比如post和get可以用同一个地址
@RestController public class CallbackController { //01 从url中获取单个参数 //localhost:8999/callback/auth @GetMapping("/callback/{auth_code}") public String getValueByUrl(@PathVariable("auth_code") String authCode) { String result = "success:" + authCode; return result; } // 02 从url中获取多个参数 //localhost:8999/callback/45646/body/46546 @GetMapping("/callback/{auth_code}/body/{state}") public String callbackRestTest(@PathVariable("auth_code") String authCode, @PathVariable("state") String state) { String result = "success:" + authCode + "," + state; return result; } // 03 从请求参数中获取值 //localhost:8999/callback?auth_code=4546 @GetMapping("/callback") public String getValueByQueryParams(@RequestParam("auth_code") String authCode) { String result = "success:" + authCode; return result; } // 04 普通的接收get请求参数 //localhost:8999/callback/v2?authCode=4546 @GetMapping("/callback/v2") public String commonGetQuery(String authCode) { String result = "success:" + authCode; return result; } }