RestFul风格就是一个资源定位和资源操作的风格,基于这种风格设计的软件可以更加简洁,更有层次,更容易实现缓存等机制
1.常规请求风格
@Controller
public class RestFulController {
// 原始://http://localhost:8080/springmvc_04_controller_war_exploded/add?a=1&b=3
@RequestMapping("/add")
public String restFul( int a, int b, Model model){
int res=a+b;
model.addAttribute("msg","结果为:"+res);
return "test";
}
}
2.RestFul风格
@Controller
public class RestFulController {
//restFul http://localhost:8080/springmvc_04_controller_war_exploded/add/1/3
@RequestMapping( "/add/{a}/{b}")
public String restFul(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","结果为:"+res);
return "test";
}
}
使用:参数用@PathVariable来注解,RequestMapping中将参数用{}加上
3.使用不同的方法(post,get,delete…)提交
@Controller
public class RestFulController {
@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.DELETE)
public String restFul(@PathVariable int a, @PathVariable int b, Model model){
int res=a+b;
model.addAttribute("msg","结果为:"+res);
return "test";
}
}
再RequestMapping中,加上method属性值
或者,直接将RequestMapping改用以下值:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping