返回结果ResponseResult类封装
public class ResponseResult {
/**
* 状态
*/
private int code;
/**
* 返回是否成功
*/
private Boolean success;
/**
* 消息
*/
private String message;
/**
* 数据对象
*/
private Object data;
public ResponseResult(TypeUtils responseStatusEnum) {
this.code = responseStatusEnum.getCode();
this.message = responseStatusEnum.getMessage();
}
/**
* 成功返回
*/
public static ResponseResult SUCCESS() {
ResponseResult responseResult = new ResponseResult(TypeUtils.SUCCESS);
responseResult.setSuccess(true);
return responseResult;
}
/**
* 成功返回,带消息说明
* @param message 消息
*/
public static ResponseResult SUCCESS(String message) {
ResponseResult responseResult = new ResponseResult(TypeUtils.SUCCESS);
responseResult.setMessage(message);
responseResult.setSuccess(true);
return responseResult;
}
/**
* 失败返回
*/
public static ResponseResult FAILED() {
ResponseResult responseResult = new ResponseResult(TypeUtils.FAILED);
responseResult.setSuccess(false);
return responseResult;
}
/**
* 失败返回,带消息说明
* @param message 消息
*/
public static ResponseResult FAILED(String message) {
ResponseResult responseResult = new ResponseResult(TypeUtils.FAILED);
responseResult.setMessage(message);
responseResult.setSuccess(false);
return responseResult;
}
/**
* 服务器内部出错
*/
public static ResponseResult SERVER_ERROR() {
ResponseResult responseResult = new ResponseResult(TypeUtils.FAILED);
responseResult.setSuccess(false);
return responseResult;
}
/**
* 服务器内部出错
* @param message 消息
*/
public static ResponseResult SERVER_ERROR(String message) {
ResponseResult responseResult = new ResponseResult(TypeUtils.SERVER_ERROR);
responseResult.setMessage(message);
responseResult.setSuccess(false);
return responseResult;
}
public ResponseResult setData(Object data) {
this.data = data;
return this;
}
}
枚举类TypeUtils封装
public enum TypeUtils {
/**
* 请求成功
*/
SUCCESS(200, "请求成功"),
/**
* 请求失败
*/
FAILED(400, "请求失败"),
/**
* 接口不存在
*/
NOT_FOUND(404, "接口不存在"),
/**
* 服务器内部出错
*/
SERVER_ERROR(500, "服务器内部出错");
private TypeUtils(int code, String message) {
this.code = code;
this.message = message;
}
/**
* 状态
*/
private int code;
/**
* 消息
*/
private String message;
/**
* 状态
*/
public int getCode() {
return code;
}
/**
* 状态
*/
public void setCode(int code) {
this.code = code;
}
/**
* 消息
*/
public String getMessage() {
return message;
}
/**
* 消息
*/
public void setMessage(String message) {
this.message = message;
}
}
使用方式Controller
@GetMapping("test")
public ResponseResult test(String data)throws Exception {
return ResponseResult.SUCCESS("成功").setData(data);
}