1、响应体封装类
package com.lms.demo.fileservice.util;
import java.io.Serializable;
/**
* @Author: 李孟帅
* @CreateTime: 2020-12-24 14:09
* @Description: 响应结果封装
*/
public class ResponseResult<T> implements Serializable {
private static final long serialVersionUID = 4359709211352400087L;
private final Integer status;
private final String message;
private final T data;
public ResponseResult(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
public static <T> ResponseResult<T> success() {
return ResponseResult.result(StatusCode.S_200,StatusCode.S_200.message,null);
}
public static <T> ResponseResult<T> success(T data) {
return ResponseResult.result(StatusCode.S_200,StatusCode.S_200.message,data);
}
public static <T> ResponseResult<T> success(String message, T data) {
return ResponseResult.result(StatusCode.S_200,message,data);
}
public static <T> ResponseResult<T> error() {
return ResponseResult.result(StatusCode.S_500,StatusCode.S_500.message,null);
}
public static <T> ResponseResult<T> error(T data) {
return ResponseResult.result(StatusCode.S_500,StatusCode.S_500.message,data);
}
public static <T> ResponseResult<T> error(String message, T data) {
return ResponseResult.result(StatusCode.S_500,message,data);
}
public static <T> ResponseResult<T> result(int status, String message) {
return ResponseResult.result(status,message,null);
}
public static <T> ResponseResult<T> result(StatusCode statusCode, String message, T data) {
return ResponseResult.result(statusCode.code, message, data);
}
public static <T> ResponseResult<T> result(int status, String message, T data) {
return new ResponseResult<>(status, message, data);
}
public Integer getStatus() {
return status;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
@Override
public String toString() {
return "ResponseResult{" +
"status=" + status +
", message='" + message + '\'' +
", data=" + data +
'}';
}
public enum StatusCode {
/**
* @Author 李孟帅
* @Date 2020/12/24 14:43
* @Description 响应状态码
*/
S_200("OK",200),
S_201("Created",201),
S_202("Accepted",202),
S_204("Not Content",204),
S_400("Bad Request",400),
S_401("Unauthorized",401),
S_403("Forbidden",403),
S_404("Not Found",404),
S_408("Request Timeout",408),
S_409("Conflict",409),
S_415("Unsupported Media Type",415),
S_500("Internal Server Error",500),
S_502("Bad Gateway",502);
private final String message;
private final int code;
private StatusCode(String message,int code){
this.message=message;
this.code=code;
}
public String getMessage(){
return message;
}
public int getCode() {
return code;
}
}
}