@PathVariable注解
学习RESTful风格首先要了解@PathVariable注解
@PathVariable:路径变量,可以将变量直接加入到路径中
具体代码
@Controller
public class HelloController {
@RequestMapping("hello/{a}/{b}")
public String test2(@PathVariable int a,@PathVariable int b, Model model){
int res = a+b;
model.addAttribute("msg","结果为:"+res);
return "test";
}
}
这里将 @PathVariable 放在变量前,表示访问时变量可以作为路径的形式出现
例:http://localhost:8080/hello/1/2
页面显示为结果为:3
一个简单的RESTful风格的url就完成了
此外@RequestMapping可以指定以什么请求方式进行访问
具体代码
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.GET )
@Controller
public class HelloController {
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.GET )
public String test2(@PathVariable int a,@PathVariable int b, Model model){
int res = a+b;
model.addAttribute("msg","结果为:"+res);
return "test";
}
}
通过method属性来设置请求方式,可以将其设置为
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.GET )
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.POST)
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.PUT)
@RequestMapping(value = "hello/{a}/{b}",method = RequestMethod.DELETE)
// ......还有一些没有了解到的日后补充
默认访问方法为GET方法
或者以另一种方式指定请求方式
@GetMapping //以GET方式进行访问,查询
@PostMapping //新增
@PutMapping //更新
@DeleteMapping //删除
@PatchMapping //对局部更新
OK!