自定义通用接口返回类
程序员文章站
2022-04-21 15:25:47
一. 定义枚举类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
上一篇: 新光驱不读盘:保养要重视防潮工作
下一篇: Java对接PayPal进行付款
推荐阅读
-
FastJson序列化自定义返回字段,普通类从spring容器中获取bean
-
如何使用Serializable接口来自定义PHP中类的序列化
-
Java和.Net版通用工具类实现--生成自定义Web Html/Excel测试用例和测试报告
-
Java Springboot API接口设计模式通用返回类
-
spring boot2.x 后端参数校验+统一异常处理+后端自定义全局统一接口返回响应数据格式
-
Spring Data JPA实现查询结果返回map或自定义的实体类
-
jolt 自定义函数实现通用接口结果体返回
-
rest接口封装统一返回数据工具类
-
REST接口封装统一返回数据工具类
-
Tomcat与JavaWeb 8.1 自定义JSP标签及其主要接口、类介绍