欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自定义通用接口返回类

程序员文章站 2024-01-17 17:08:40
一. 定义枚举类ResultCodeEnum(可以在返回类类内定义,也可以额外定义,个人喜欢分开定义)package com.shixin.utils.enums;import lombok.AllArgsConstructor;import lombok.NoArgsConstructor;/** * @author shixin * @date 2020/11/11 15:16 */@AllArgsConstructor@NoArgsConstructorpublic en...

一. 定义枚举类ResultCodeEnum(可以在返回类类内定义,也可以额外定义,个人喜欢分开定义)

package com.shixin.utils.enums;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

/**
 * @author shixin
 * @date 2020/11/11 15:16
 */

@AllArgsConstructor
@NoArgsConstructor
public enum  ResultCodeEnum {
    /*** 通用部分 ***/
    // 通用成功请求
    SUCCESS(0, "请求成功"),
    //通用失败请求
    FAIL(1, "请求失败"),

    ;
    /**
     * 响应状态码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

二.通用返回类Result

package com.shixin.utils;

import com.shixin.utils.enums.ResultCodeEnum;
import lombok.Data;

/**
 * @author shixin
 * @date 2020/11/11 15:23
 */

@Data
public class Result<T> implements java.io.Serializable {

    private int code;
    private String msg;
    private T data;

    /**
     *@param
     *@desc 自定义code,message,也可以在ResultCodeEnum中提前规定
     */
    public static <T> Result<T> custom(int code, String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     *@param
     *@desc 自定义code,message,也可以在ResultCodeEnum中提前规定
     */
    public static Result<Object> custom(int code, String msg) {
        Result<Object> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }


    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(msg == null ? ResultCodeEnum.SUCCESS.getMessage() : msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }

    public static Result<Object> success(String msg) {
        Result<Object> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(msg == null ? ResultCodeEnum.SUCCESS.getMessage() : msg);
        return result;
    }

    public static <T> Result<T> fail(String msg, T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMsg(msg == null ? ResultCodeEnum.FAIL.getMessage() : msg);
        result.setData(data);
        return result;
    }

    public static Result<Object> fail(String msg) {
        Result<Object> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMsg(msg == null ? ResultCodeEnum.FAIL.getMessage() : msg);
        return result;
    }

    public static Result<Throwable> error(Throwable e) {
        Result<Throwable> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL.getCode());
        result.setMsg("服务器繁忙,请稍候再试");
        result.setData(e);
        return result;
    }


}

三.测试类TestApiController

package com.shixin.api.controller;

import com.shixin.entity.Test;
import com.shixin.service.TestService;
import com.shixin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

/**
 * @author shixin
 * @date 2020/11/11 15:48
 */

@RestController
public class TestApiController {

    @RequestMapping(value = "/apiget/{id}",method = RequestMethod.GET)
    public Result<Test> testResult(@PathVariable Integer id){
        Test test = new Test();
        test.setId(id);
        test.setMagicId("test");
        test.setFirstName("firstName");
        test.setLastName("lastName");
        return Result.success(test);
    }

}

四.返回结果

自定义通用接口返回类

本文地址:https://blog.csdn.net/weixin_38230747/article/details/109638440