RestFul 风格

RestFul 风格

  • Restful就是一个资源定位及资源操作的风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
  • 互联网所有的事物都可以被抽象为资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。 分别对应 添加、删除、修改、查询。
  • 传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get

原来的:http://localhost:8080/springmvc_05_controller_war_exploded/add?a=1&b=2
RestFul风格:http://localhost:8080/springmvc_05_controller_war_exploded/add/1/2


@Controller
public class RestFulController {

    //@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    @GetMapping("/add/{a}/{b}")   //表示get请求
    public String test1(@PathVariable int a, @PathVariable int b, Model model){

        int res = a + b;
        model.addAttribute("msg","结果1为"+res);
        return "test";
    }

    //相当于:@RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.POST)
    @PostMapping("/add{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model){

        int res = a + b;
        model.addAttribute("msg","结果2为"+res);
        return "test";
    }


}

在这里插入图片描述
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping

@GetMapping 是一个组合注解
它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。
平时使用的会比较多!

posted @ 2020-07-30 16:31  iucbjb  阅读(67)  评论(0)    收藏  举报