1、结构:
1个控制层类+1个枚举+1个数据处理
2、内容:
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
</dependencies>
ResData.java
package com.xcl.ResData;
import lombok.Data;
@Data //get+set方法
@NoArgsConstructor //无参构造函数
@AllArgsConstructor //有参构造函数
public class ResData<T> {
private Integer code;
private String msg;
private T data;
public static <T> ResData setResult(DataEnum enums,T data) {
return new <T>ResData(enums.getResCode(), enums.getResMsg(),data);
}
}
DataEnum.java
package com.xcl.ResData;
@AllArgsConstructor
public enum DataEnum {
SUCCESS(0, "成功"),
;
private Integer resCode;
private String resMsg;
public Integer getResCode() {
return this.resCode;
}
public String getResMsg() {
return this.resMsg;
}
}
demotroller.java
package com.xcl.controller;
import com.xcl.ResData.DataEnum;
import com.xcl.ResData.ResData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
@RestController
public class demotroller {
@GetMapping("/data")
public <T>ResData data() {
return ResData.setResult(DataEnum.SUCCESS,null);
}
}
3、测试: