springboot 后台Api设计

首先定义一个枚举类,枚举状态码和消息

package com.hxut.domain;

/**
 * description: Response
 * date: 2022/5/23 21:09
 * author: MR.孙
 */
public class Response {

    public enum Code{
        //1001-1099成功
        SUCCESS(1001,"成功"),
        SAVE_SUCCESS(1002,"保存成功"),
        DELETE_SUCCESS(1003,"删除成功"),
        UPDATE_SUCCESS(1004,"修改成功"),
        SELECT_SUCCESS(1005,"查询成功"),
        //1101-1199失败
        FAILED(1101,"失败"),
        SAVE_FAILED(1102,"保存失败"),
        DELETE_FAILED(1103,"删除失败"),
        UPDATE_FAILED(1104,"修改失败"),
        SELECT_FAILED(1105,"查询失败"),
        //1201-1299参数错误或为空
        PARAM_IS_NULL(1201,"参数为空"),
        PARAM_IS_ERROR(1202,"参数错误")
        ;
        private Integer code;
        private String msg;

        Code(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        public Integer code() {
            return code;
        }

        public String msg() {
            return msg;
        }

    }
}

然后用一个泛型实体类进行封装,分别包含了状态码,消息,数据等成员变量。
先对泛型实体类进行初始化啊

   public static <T> Result<T> restResult(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;

    }

然后根据需求,生成不同的构造方法

 public static <T> Result<T> ok(){
        return restResult(Response.Code.SUCCESS.code(),Response.Code.SUCCESS.msg(),null);
    }
    
    public static <T> Result<T> ok(T data){
        return restResult(Response.Code.SUCCESS.code(),Response.Code.SUCCESS.msg(),data);
    }

    public static <T> Result<T> ok(Integer code,T data){
        return restResult(code,Response.Code.SUCCESS.msg(),data);
    }

    public static <T> Result<T> ok(Integer code,String msg,T data){
        return restResult(code,msg,data);
    }

    public static <T> Result<T> failed(){
        return restResult(Response.Code.FAILED.code(),Response.Code.FAILED.msg(),null);
    }

    public static <T> Result<T> failed(String msg){
        return restResult(Response.Code.FAILED.code(),msg,null);
    }

    public static <T> Result<T> failed(Integer code,String msg,T data){
        return restResult(code,msg,data);
    }


    public static <T> Result<T> failed(Integer code,String msg){
        return restResult(code,msg,null);
    }


    public static <T> Result<T> failed(T data){
        return restResult(Response.Code.FAILED.code(),Response.Code.FAILED.msg(),data);
    }

完整代码:

package com.hxut.domain;

import com.baomidou.mybatisplus.extension.api.R;
import lombok.Data;

/**
 * description: Result
 * date: 2022/5/23 21:09
 * author: MR.孙
 */
@Data
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;

    public Result() {
    }


   /**
   * @param code 状态码
    * @param msg 消息
    * @param data 数据
   *@description: 初始化
   *@return: com.hxut.domain.Result<T>
   *@author: MR.孙
   *@date: 2022/5/23 21:54
   **/
    public static <T> Result<T> restResult(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;

    }

    public static <T> Result<T> ok(){
        return restResult(Response.Code.SUCCESS.code(),Response.Code.SUCCESS.msg(),null);
    }
    
    public static <T> Result<T> ok(T data){
        return restResult(Response.Code.SUCCESS.code(),Response.Code.SUCCESS.msg(),data);
    }

    public static <T> Result<T> ok(Integer code,T data){
        return restResult(code,Response.Code.SUCCESS.msg(),data);
    }

    public static <T> Result<T> ok(Integer code,String msg,T data){
        return restResult(code,msg,data);
    }

    public static <T> Result<T> failed(){
        return restResult(Response.Code.FAILED.code(),Response.Code.FAILED.msg(),null);
    }

    public static <T> Result<T> failed(String msg){
        return restResult(Response.Code.FAILED.code(),msg,null);
    }

    public static <T> Result<T> failed(Integer code,String msg,T data){
        return restResult(code,msg,data);
    }


    public static <T> Result<T> failed(Integer code,String msg){
        return restResult(code,msg,null);
    }


    public static <T> Result<T> failed(T data){
        return restResult(Response.Code.FAILED.code(),Response.Code.FAILED.msg(),data);
    }


    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}

Service层:为什么在service层对结果进行封装呢,因为在service层进行封装之后,不影响controller层的易读性把。

package com.hxut.service.impl;


import com.hxut.domain.Book;
import com.hxut.domain.Response;
import com.hxut.domain.Result;
import com.hxut.mapper.BookMapper;
import com.hxut.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * description: BookServiceImpl
 * date: 2022/5/23 23:39
 * author: MR.孙
 */
@Service
public class BookServiceImpl implements IBookService {
    @Autowired
    private BookMapper bookMapper;
    public Result<Boolean> insert(Book book){
        Boolean data = bookMapper.insert(book)>0;
        return Result.ok(Response.Code.SAVE_SUCCESS.code(),Response.Code.SAVE_SUCCESS.msg(),data);
    }

    @Override
    public Result<Boolean> update(Book book) {
        Boolean data = bookMapper.updateById(book) > 0;
        return Result.ok(Response.Code.UPDATE_SUCCESS.code(),Response.Code.UPDATE_SUCCESS.msg(),data);
    }

    @Override
    public Result<Boolean> delete(Integer id) {
        Boolean data = bookMapper.deleteById(id) > 0;
        return Result.ok(Response.Code.DELETE_SUCCESS.code(),Response.Code.DELETE_SUCCESS.msg(),data);
    }

    @Override
    public Result<List<Book>> getAll() {
        List<Book> data = bookMapper.selectList(null);
        return Result.ok(Response.Code.SELECT_SUCCESS.code(),Response.Code.SELECT_SUCCESS.msg(),data);

    }

}


可以看到controller还是service的调用,而service进行了封装。

posted @ 2022-05-24 18:27  长情c  阅读(135)  评论(0)    收藏  举报