- HttpEnum
package com.khbd.lps.cost.integrator.utils.http;
import lombok.Getter;
/**
* http response enum
*/
public enum HttpEnum {
SUCCESS(200, "success"), ERROR(500, "fail");
/**
* 状态码
*/
@Getter
private int code;
/**
* 备注
*/
@Getter
private String remark;
HttpEnum(int code, String remark) {
this.code = code;
this.remark = remark;
}
}
ApiResponse
package com.khbd.lps.cost.integrator.utils.http;
/**
* http统一响应封装
*/
public class ApiResponse {
/**
* 状态码
*/
private int code;
/**
* 数据
*/
private Object data;
/**
* 报文描述
*/
private String message;
public ApiResponse(int code, Object data, String message) {
this.code = code;
this.data = data;
this.message = message;
}
private ApiResponse(int code, String message) {
this.code = code;
this.message = message;
}
/**
* 无参空构造
*/
private ApiResponse() {
}
/**
* 成功
*
* @param data 数据
* @return
*/
public static ApiResponse success(Object data) {
return new ApiResponse(HttpEnum.SUCCESS.getCode(), data, HttpEnum.SUCCESS.getRemark());
}
/**
* 成功
*
* @return
*/
public static ApiResponse success() {
return new ApiResponse(HttpEnum.SUCCESS.getCode(), HttpEnum.SUCCESS.getRemark());
}
/**
* 失败
*
* @return
*/
public static ApiResponse error() {
return new ApiResponse(HttpEnum.ERROR.getCode(), HttpEnum.ERROR.getRemark());
}
/**
* 失败
*
* @param message 消息
* @return
*/
public static ApiResponse error(String message) {
return new ApiResponse(HttpEnum.ERROR.getCode(), message);
}
public static ApiResponse error(int code,Object data,String message) {
return new ApiResponse(code,data, message);
}
public int getCode() {
return code;
}
public void setStatusCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void setCode(int code) {
this.code = code;
}
}
- 使用
return ApiResponse.success();
return ApiResponse.success(xxxx);
return ApiResponse.error();
return ApiResponse.error(xxxx);
也可以自定义状态码