美文网首页
RESTful架构风格

RESTful架构风格

作者: paven559 | 来源:发表于2018-06-12 10:21 被阅读0次

    前言:前几天面试,面试官问我了解RESTful吗?我确实使用过,并且认为这样的风格使得系统的访问路径整洁 ,不同的请求只需要根据请求方式的不同去执行对应的操作。

    1.RESTful是什么

    RESTful, 全称Representational State Transfer。

    REST通常基于使用HTTP,URI,XML,JSON,HTML等广泛流行的标准和协议;轻量级,跨平台,跨语言的架构设计;它是一种设计风格,不是一种标准,是一种思想。

    2.为什么会出现RESTful

    在Restful之前的操作:

    http://127.0.0.1/user/query/1 GET 根据用户id查询用户数据

    http://127.0.0.1/user/save POST 新增用户

    http://127.0.0.1/user/update POST 修改用户信息

    http://127.0.0.1/user/delete GET/POST 删除用户信息

    RESTful用法:

    http://127.0.0.1/user/1 GET 根据用户id查询用户数据

    http://127.0.0.1/user POST 新增用户

    http://127.0.0.1/user PUT 修改用户信息

    http://127.0.0.1/user DELETE 删除用户信息

    之前的操作是没有问题的,但你每次请求的接口或者地址,都在做描述,例如查询的时候用了query,新增的时候用了save,其实完全没有这个必要,我使用了get请求,就是查询.使用post请求,就是新增的请求,我的意图很明显,完全没有必要做描述,这就是为什么有了restful.

    3.使用方式:

    image

    URI设计使用技巧

    使用_或-来让URI可读性更好,例如http://www.github.com/blog/translate-reward-plan

    使用/来表示资源的层级关系,例如上面的/git/git/blob/master/block-sha1/sha1.h

    使用?用来过滤资源,例如/git/pulls?state=closed用来表示git项目的所有推入请求中已经关闭的请求。

    使用,或;表示同级资源关系,例如/git/sha1/compare/ef7b53d18;bd638e8c1

    示例代码

    
    @RequestMapping("/user")  
    @Controller  
    public class RestUserController {  
      
        @Autowired  
        private NewUserService newUserService;  
      
        /** 
         * 根据用户id查询用户数据 
         *  
         * @param id 
         * @return 
         */  
        @RequestMapping(value = "{id}", method = RequestMethod.GET)  
        @ResponseBody  
        public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {  
            try {  
                User user = this.newUserService.queryUserById(id);  
                if (null == user) {  
                    // 资源不存在,响应404  
                    return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);  
                }  
                // 200  
                // return ResponseEntity.status(HttpStatus.OK).body(user);  
                return ResponseEntity.ok(user);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            // 500  
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
        }  
      
        /** 
         * 新增用户 
         *  
         * @param user 
         * @return 
         */  
        @RequestMapping(method = RequestMethod.POST)  
        public ResponseEntity<Void> saveUser(User user) {  
            try {  
                this.newUserService.saveUser(user);  
                return ResponseEntity.status(HttpStatus.CREATED).build();  
            } catch (Exception e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            // 500  
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
        }  
      
        /** 
         * 更新用户资源 
         *  
         * @param user 
         * @return 
         */  
        @RequestMapping(method = RequestMethod.PUT)  
        public ResponseEntity<Void> updateUser(User user) {  
            try {  
                this.newUserService.updateUser(user);  
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            // 500  
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
        }  
      
        /** 
         * 删除用户资源 
         *  
         * @param user 
         * @return 
         */  
        @RequestMapping(method = RequestMethod.DELETE)  
        public ResponseEntity<Void> deleteUser(@RequestParam(value = "id", defaultValue = "0") Long id) {  
            try {  
                if (id.intValue() == 0) {  
                    // 请求参数有误  
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();  
                }  
                this.newUserService.deleteUserById(id);  
                // 204  
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            // 500  
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);  
        }  
    }  
    
    

    Http状态码

    20170625152145836.png

    声明转载来源

    https://blog.csdn.net/qq_33489669/article/details/56479485
    https://blog.csdn.net/chenxiaochan/article/details/73716617

    相关文章

      网友评论

          本文标题:RESTful架构风格

          本文链接:https://www.haomeiwen.com/subject/cgcleftx.html