其他分享
首页 > 其他分享> > Rest风格

Rest风格

作者:互联网

Rest风格

Rest简介

REST风格简介

上诉行为是约定方式,约定不是规范,可以打破,所以称为REST风格,而不是REST规范

描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts......

RESTful快速入门

  1. 设定http请求动作(动词)
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String update(@PathVariable User user){
    System.out.println("user delete..."+user);
    return "{'module':'user update'}";
}
@RequestMapping(value = "/users",method = RequestMethod.PUT)
    @ResponseBody
    public String update(@PathVariable User user){
        System.out.println("user delete..."+user);
        return "{'module':'user update'}";
    }
  1. 设置请求参数(路径变量)
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
    System.out.println("user delete..."+id);
    return "{'module':'user delete'}";
}

@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
    System.out.println("user delete..."+id);
    return "{'module':'user delete'}";
}
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
    System.out.println("user delete..."+id);
    return "{'module':'user delete'}";
}

RESTful快速开发

//Rest模式
@RestController//相当于@ResponseBody+@Controller
@RequestMapping( "/books")
public class BookController {

    //@RequestMapping( method = RequestMethod.POST)
    @PostMapping
    public String save(){
        System.out.println("user save...");
        return "{'module':'user save'}";
    }
    //@RequestMapping(value = " /{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){
        System.out.println("user delete..."+id);
        return "{'module':'user delete'}";
    }
    //@RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    public String update(@PathVariable User user){
        System.out.println("user delete..."+user);
        return "{'module':'user update'}";
    }
    //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
       System.out.println("springboot is running3...");
       return "springboot is running3...";
    }
}

标签:RequestMapping,http,PathVariable,Rest,风格,user,id,delete
来源: https://www.cnblogs.com/hp-cn/p/16139870.html