Rest风格

导读:本篇文章讲解 Rest风格,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Rest风格

一、Rest风格概论

Rest,表先形式状态转化

传统风格资源描述形式:

  • http://localhost/user/getById?id=1

而Rest风格描述形式:

  • http://localhost/user/1

显而易见,Rest风格有以下优势:

  • 隐藏资源的访问行为,无法通过地址得知对资源是哪种操作
  • 简化书写

二、Rest风格入门

Rest风格访问资源时使用行为动作区分对资源进行了哪种操作

url 作用 请求方式
http://localhost/user 查询全部用户的信息 GET(查询)
http://localhost/user/1 查询指定用户的信息 GET(查询)
http://localhost/users 添加用户信息 POST(新增/保存)
http://localhost/users 修改用户信息 PUT(修改/更新)
http://localhost/user/1 删除用户信息 DELETE(删除)

以上的行为均为约定方式

所谓约定方式就是大家说好了的,不是规范可以打破,所以称为REST风格,而不是REST规范

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

根据Rest风格对资源的访问称为Restful


三、Rest代码操作

案例:将以下代码改装为Rest风格


@Controller
public class bookController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        System.out.println("user save....");
        return "{'module':'user save'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        System.out.println("user delete...." + id);
        return "{'module':'user delete'}";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestBody User user){
        System.out.println("user update...." + user);
        return "{'module':'user update'}";
    }

    @RequestMapping("/getById")
    @ResponseBody
    public String getById(int id){
        System.out.println("user getById...." + id);
        return "{'module':'user getById'}";
    }

}

注意:

  • @RequestBody接受的是一个json格式的字符串,一定是一个json对象字符串。
  • 在使用springmvc的做项目时,将JSON字符串和JSON对象混淆了。
  • .@RequestParam接受单个参数,参数位置位于链接后。@RequestBody接受JSON对象字符串,参数位置位于请求体。

初步修改

@Controller
public class bookController {

    @RequestMapping(value = "/books", method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        System.out.println("user save....");
        return "{'module':'user save'}";
    }

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

    @RequestMapping(value = "/books", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user){
        System.out.println("user update...." + user);
        return "{'module':'user update'}";
    }

    @RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable int id){
        System.out.println("user getById...." + id);
        return "{'module':'user getById'}";
    }

}

这里的修改主要是制定了是请求方式和进行参数绑定

但是这里的重复代码依然是很多

简化操作:

  • 将/books提到外面去
  • 每个方法都有@ResponseBody,将@ResponseBody也提到外面(注意如果里面有某个方法不带@ResponseBody,则不可提到外面去)
  • 这时候外面有三个注解@Controller、@ResponseBody、 @RequestMapping,则可以用@RestController代替@Controller和@ResponseBody
  • 将Post等请求改为相应注解
// @Controller
// @ResponseBody
@RestController
@RequestMapping("/books")
public class bookController {

    // @RequestMapping(method = RequestMethod.POST)
    @PostMapping
    public String save(){
        System.out.println("user save....");
        return "{'module':'user save'}";
    }

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

    // @RequestMapping( method = RequestMethod.PUT)
    @PutMapping
    public String update(@RequestBody User user){
        System.out.println("user update...." + user);
        return "{'module':'user update'}";
    }

    // @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @GetMapping("/{id}")
    public String getById(@PathVariable int id){
        System.out.println("user getById...." + id);
        return "{'module':'user getById'}";
    }

}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95019.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!