SpringBoot学习(八)--SpringBoot中Restful最佳实践

版权声明:作者原创,转载请注明出处。
本系列文章目录地址:http://blog.****.net/u011961421/article/details/79416510

简介

REST:英文representational state transfer直译为表现层状态转移,或者表述性状态转移;Rest是web服务的一种架构风格,一种设计风格,是一种思想;同时Rest不是针对某一种编程语言的。
以webService为例通俗解释。
非Rest设计,以往我们都会这么写:
http://localhost:8080/admin/getUser (查询用户)
http://localhost:8080/admin/addUser (新增用户)
http://localhost:8080/admin/updateUser (更新用户)
http://localhost:8080/admin/deleteUser (删除用户)
总结:以不同的URL(主要为使用动词)进行不同的操作。

Rest架构:
GET http://localhost:8080/admin/user (查询用户)
POST http://localhost:8080/admin/user (新增用户)
PUT http://localhost:8080/admin/user (更新用户)
DELETE http://localhost:8080/admin/user (删除用户)
总结:URL只指定资源,以HTTP方法动词进行不同的操作。用HTTP STATUS/CODE定义操作结果。

Restful:遵守了rest风格的web服务便可称为Restful。

为什么需要Restful?

  • URL具有很强可读性的,具有自描述性
  • 规范化请求过程和返回结果
  • 资源描述与视图的松耦合
  • 可提供OpenAPI,便于第三方系统集成,提高互操作性
  • 提供无状态的服务接口,降低复杂度,可提高应用的水平扩展性

实战

在之前SpringBoot学习系列的工程基础上,添加Restful API。Restful的核心在controller,以用户的增删改查为例,代码如下

package com.pf.org.cms.web;

import com.alibaba.fastjson.JSONObject;
import com.pf.org.cms.common.IConstants;
import com.pf.org.cms.entity.JsonBean;
import com.pf.org.cms.entity.UserInfo;
import com.pf.org.cms.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.Map;

/**
 * @Auther: pf
 * @Date: 2018/2/27 19:52
 * @Description:
 */
@RestController
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "getUser", notes = "管理员接口获取用户")
    @RequestMapping(method = RequestMethod.GET, value = "/user")
    public String getUser(@RequestParam long id) {
        JsonBean reData = new JsonBean();
        Map user = userService.getUserInfo(id);
        reData.setStatus(IConstants.RESULT_INT_SUCCESS);
        reData.setMessage("查询成功");
        reData.setData(user);
        return JSONObject.toJSONString(reData);
    }

    @ApiOperation(value = "addUser", notes = "管理员接口新增用户")
    @RequestMapping(method = RequestMethod.POST, value = "/user")
    public String addUser(@RequestParam String user) {
        JsonBean reData = new JsonBean();
        UserInfo userInfo = (UserInfo) JSONObject.parseObject(user, UserInfo.class);
        if (userService.addUserInfo(userInfo)) {
            reData.setStatus(IConstants.RESULT_INT_SUCCESS);
            reData.setMessage("新增成功");
        } else {
            reData.setStatus(IConstants.RESULT_INT_ERROR);
            reData.setMessage("新增失败");
        }
        return JSONObject.toJSONString(reData);
    }

    @ApiOperation(value = "updateUser", notes = "管理员接口更新用户")
    @RequestMapping(method = RequestMethod.PUT, value = "/user")
    public String updateUser(@RequestParam String user) {
        JsonBean reData = new JsonBean();
        UserInfo userInfo = (UserInfo) JSONObject.parseObject(user, UserInfo.class);
        if (userService.updateUserInfo(userInfo)) {
            reData.setStatus(IConstants.RESULT_INT_SUCCESS);
            reData.setMessage("更新成功");
        } else {
            reData.setStatus(IConstants.RESULT_INT_ERROR);
            reData.setMessage("更新失败");
        }
        return JSONObject.toJSONString(reData);
    }

    @ApiOperation(value = "delUser", notes = "管理员接口删除用户")
    @RequestMapping(method = RequestMethod.DELETE, value = "/user")
    public String delUser(@RequestParam long id) {
        JsonBean reData = new JsonBean();
        if (userService.deleteUserInfo(id)) {
            reData.setStatus(IConstants.RESULT_INT_SUCCESS);
            reData.setMessage("删除成功");
        } else {
            reData.setStatus(IConstants.RESULT_INT_ERROR);
            reData.setMessage("删除失败");
        }
        return JSONObject.toJSONString(reData);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

@RestController作用相当于@ResponseBody + @Controller
这里的在线API工具使用的是SpringBoot学习(六)中介绍的swagger,@ApiOperation为swagger中的注解。

UserService这里不做介绍,即为基础的增删改查服务,运行工程,访问swagger-ui.html页面后结果为:
SpringBoot学习(八)--SpringBoot中Restful最佳实践
根据自己的测试数据测试一下查询接口:
SpringBoot学习(八)--SpringBoot中Restful最佳实践
SpringBoot学习(八)--SpringBoot中Restful最佳实践
也可以再测试一下新增用户的接口
SpringBoot学习(八)--SpringBoot中Restful最佳实践

最后,Restful的实现并不复杂,最重要的是需要理解Restful的思想,并且在架构设计好后更加规范的编码执行。本文及本系列项目工程地址:https://github.com/15651037763/cms

版权声明:作者原创,转载请注明出处。
本系列文章目录地址:http://blog.****.net/u011961421/article/details/79416510