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 删除用户信息

说明:查询用get,新增用post,修改用put,删除用delete

案例:

package cn.itcast.mybatis.controller;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
     
    import cn.itcast.mybatis.pojo.User;
    import cn.itcast.mybatis.service.NewUserService;
     
    @RequestMapping("restful/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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值