1.获取连接中的参数,使用倒的关键词@PathVariable
@RestController public class HelloController {
1@RequestMapping(value = "/hello/{id}",method = RequestMethod.GET) 2public String index(@PathVariable("id") Integer id){ 3 return "id="+id; 4}
}
启动项目访问,成功获取到id

也可以把id放在/hello前面
1public class HelloController { 2 3 @RequestMapping(value = "/{id}/hello",method = RequestMethod.GET) 4 public String index(@PathVariable("id") Integer id){ 5 return "id="+id; 6 } 7 8}
2.传统的问号(?id=110)传值,设置id可以不传默认值为0,使用倒的关键词@RequestParam
1public class HelloController { 2 3 @RequestMapping(value = "/hello",method = RequestMethod.GET) 4 public String index(@RequestParam(value = "id" ,required = false, defaultValue = "0") Integer id){ 5 return "id="+id; 6 } 7 8}
不传参数默认为0

传参数为获取到的参数
