SpringBoot-22-RESTful统一规范响应数据格式
什么是REST?
REST是Representational State Transfer的缩写,是在2000年被Roy Thomas Fielding提出的,Fielding是一个很厉害的人物,他是HTTP协议的主要设计者。REST是他对互联网软件构架的原则。REST是一种针对网络应用设计和软件开发方式,降低了开发的复杂性,提高了系统的可伸缩性。如果想要具体了解一下其含义可以查看一下阮一峰老师对REST理解RESTful架构。
我们在开发过程中需要有一个统一的数据返回格式,这样可以使得所有开发人员返回结果风格统一,减少前后端开发人员的沟通时间。REST只是一种标准化的开发约定,下面我们提供一个通过返回结果的实现
返回码接口
/** * 统一返回结果接口 */ public interface IResultCode { /** * 返回码 * * @return int */ int getCode(); /** * 返回消息 * * @return String */ String getMsg(); }
返回码接口实现
@Getter @AllArgsConstructor public enum ResultCode implements IResultCode{ /** * 操作成功 */ SUCCESS(200, "操作成功"), /** * 业务异常 */ FAILURE(400, "业务异常"), /** * 服务异常 */ ERROR(500, "服务异常"), /** * 参数错误 */ GLOBAL_PARAM_ERROR(4000, "参数错误"); /** * 状态码 */ final int code; /** * 消息内容 */ final String msg; }
统一响应结果的实现
@Data @Getter public class Result<T> implements Serializable { private static final long serialVersionUID = 1L; private int code; private String msg; private long time; private T data; private Result() { this.time = System.currentTimeMillis(); } private Result(IResultCode resultCode) { this(resultCode, null, resultCode.getMsg()); } private Result(IResultCode resultCode, String msg) { this(resultCode, null, msg); } private Result(IResultCode resultCode, T data) { this(resultCode, data, resultCode.getMsg()); } private Result(IResultCode resultCode, T data, String msg) { this(resultCode.getCode(), data, msg); } private Result(int code, T data, String msg) { this.code = code; this.data = data; this.msg = msg; this.time = System.currentTimeMillis(); } /** * 返回状态码 * * @param resultCode 状态码 * @param <T> 泛型标识 * @return ApiResult */ public static <T> Result<T> success(IResultCode resultCode) { return new Result<>(resultCode); } public static <T> Result<T> success(String msg) { return new Result<>(ResultCode.SUCCESS, msg); } public static <T> Result<T> success(IResultCode resultCode, String msg) { return new Result<>(resultCode, msg); } public static <T> Result<T> data(T data) { return data(data, "处理成功"); } public static <T> Result<T> data(T data, String msg) { return data(ResultCode.SUCCESS.code, data, msg); } public static <T> Result<T> data(int code, T data, String msg) { return new Result<>(code, data, data == null ? "承载数据为空" : msg); } public static <T> Result<T> fail() { return new Result<>(ResultCode.FAILURE, ResultCode.FAILURE.getMsg()); } public static <T> Result<T> fail(String msg) { return new Result<>(ResultCode.FAILURE, msg); } public static <T> Result<T> fail(int code, String msg) { return new Result<>(code, null, msg); } public static <T> Result<T> fail(IResultCode resultCode) { return new Result<>(resultCode); } public static <T> Result<T> fail(IResultCode resultCode, String msg) { return new Result<>(resultCode, msg); } public static <T> Result<T> condition(boolean flag) { return flag ? success("处理成功") : fail("处理失败"); } }
定义实体对象
@Data public class Student implements Serializable { /** * ID */ private Long id; /** * 学生姓名 */ private String name; /** * 性别默认男 */ private String sex; /** * 年龄 */ private Integer age; /** * 注册手机号 */ private String mobile; /** * 注册邮箱 */ private String email; private Date createDate; private Date updateDate; /** *是否可用(1 可用,0 删除用户) */ private Integer isEnabled; private static final long serialVersionUID = 1L; }
实现mapper
- 实现studentmapper
public interface StudentMapper { Student findById(@Param("id") Long id); List<Student> selectAll(); void updateStudent(Student student); int insertByObject(Student student); }
- 实现map的xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.learn.springboot.mapper.StudentMapper"> <resultMap id="student" type="com.learn.springboot.entity.Student"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="sex" property="sex" /> <result column="age" property="age" /> <result column="mobile" property="mobile" /> <result column="email" property="email" /> <result column="create_date" property="createDate" /> <result column="update_date" property="updateDate" /> <result column="is_enabled" property="isEnabled" /> </resultMap> <select id="findById" resultType="com.learn.springboot.entity.Student"> SELECT * FROM STUDENT WHERE ID = #{id} </select> <select id="selectAll" resultMap="student"> SELECT * FROM STUDENT </select> <insert id="insertByObject"> INSERT INTO STUDENT(NAME, SEX,AGE,EMAIL,MOBILE) VALUES(#{name}, #{sex}, #{age}, #{email}, #{mobile}) </insert> <update id="updateStudent" parameterType="com.learn.springboot.entity.Student"> UPDATE STUDENT SET NAME=#{name},SEX=#{sex},AGE=#{age},EMAIL=#{email},MOBILE=#{mobile} WHERE id=#{id} </update> </mapper>
控制层的实现
实现student的控制层
@Slf4j @RequestMapping("/student") @RestController @AllArgsConstructor public class StudentController { /** * studentService注入StudentController */ private StudentService studentService; @PostMapping("insert") public Result insertByObject(@RequestBody Student student){ Integer byObject = studentService.insertByObject(student); return Result.success(byObject.toString()); } @PostMapping("update") public Result<Student> updateStudent(@RequestBody Student student) { return Result.data(studentService.updateStudent(student)); } @GetMapping("/select/{id}") public Result<Student> findByName(@PathVariable("id") Long id) { Student byId = studentService.findById(id); return Result.data(studentService.findById(id)); } @GetMapping("/selectAll") public Result<List<Student>> selectAll() { List<Student> students = studentService.selectAll(); return Result.data(students); } }
测试
使用postman分别测试
- http://localhost:8899/student/select/11 Get方法
- http://localhost:8899/student/selectAll Get方法
测试结果如下