1.獲取鏈接中的參數,使用倒的關鍵詞@PathVariablejava
@RestController public class HelloController { @RequestMapping(value = "/hello/{id}",method = RequestMethod.GET) public String index(@PathVariable("id") Integer id){ return "id="+id; } }
啓動項目訪問,成功獲取到idapp
也能夠把id放在/hello前面ui
public class HelloController { @RequestMapping(value = "/{id}/hello",method = RequestMethod.GET) public String index(@PathVariable("id") Integer id){ return "id="+id; } }
2.傳統的問號(?id=110)傳值,設置id能夠不傳默認值爲0,使用倒的關鍵詞@RequestParamcode
public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String index(@RequestParam(value = "id" ,required = false, defaultValue = "0") Integer id){ return "id="+id; } }
不傳參數默認爲0blog
傳參數爲獲取到的參數class