封装通用结果类
556字约2分钟
2024-08-08
在项目开发中,我们通常需要一个统一的结果返回类,返回一般主要由三个内容:
code:业务处理结果编码
msg:业务处理结果说明
data:业务处理响应数据(具体由每个接口定义)
项目源码:common-result_R
创建项目
创建一个 Spring Boot 项目,你有两种方式选择:
1、根据提供的 pom.xml 文件自己创建
2、点击下载创建好的项目:common-result_O
项目中使用了
lombok
来简化代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/>
</parent>
<groupId>com.marui.common.result</groupId>
<artifactId>common-result</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- Web开发场景所需的所有依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 简化代码 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
通用结果类
成功编码默认为000000
,错误编码默认为500
,可根据自己具体情况定义。
package com.marui.common.result.common;
import lombok.Data;
import java.io.Serializable;
/**
* @ClassName ResultDTO
* @Desciption
* @Author MaRui
* @address https://maruinotes.com
* @Date 2022/2/10 9:46
* @Version 1.0
*/
@Data
public class ResultDTO<T> implements Serializable {
/**
* 业务处理结果编码
*/
private String code;
/**
* 业务处理结果信息
*/
private String msg;
/**
* 业务处理响应数据
*/
private T data;
public ResultDTO() {
}
public ResultDTO(String code) {
this.code = code;
}
public ResultDTO(String code, String msg) {
this.code = code;
this.msg = msg;
}
public ResultDTO(String code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> ResultDTO<T> ok() {
return new ResultDTO("000000", "Success");
}
public static <T> ResultDTO<T> ok(T data) {
return new ResultDTO("000000", "Success", data);
}
public static <T> ResultDTO<T> ok(String code, String msg, T data) {
return new ResultDTO(code, msg, data);
}
public static <T> ResultDTO<T> error() {
return new ResultDTO("500", "未知异常,请联系管理员");
}
public static <T> ResultDTO<T> error(String msg) {
return new ResultDTO("500", msg);
}
public static <T> ResultDTO<T> error(String code, String msg) {
return new ResultDTO(code, msg);
}
}
测试
package com.marui.common.result.controller;
import com.marui.common.result.common.ResultDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName UserController
* @Desciption
* @Author MaRui
* @address https://maruinotes.com
* @Date 2022/2/10 9:55
* @Version 1.0
*/
@RestController
@RequestMapping(value = "/user")
public class UserController {
@GetMapping(value = "/sayHello")
public ResultDTO<String> sayHello(@RequestParam String name) {
return ResultDTO.ok("你好," + name);
}
}