一、绪论
JAVA接口返回信息ServerHttpResponse response
等登录接口token过期时需要给前端返回相关状态码和状态信息
二、Mono<Void>向前端返回没有登陆或登录超时 暂无权限访问信息组装
返回Mono对象
public abstract class Mono<T> implements CorePublisher<T>
/**
* 无权限
*
* @param response
* @return
*/
private Mono<Void> noLogin(ServerHttpResponse response) {
log.info("没有登陆或登录超时 暂无权限访问===========");
String responseDataStr = JSON
.toJSON(ResponseData.error(HttpStatus.UNAUTHORIZED.value(), "The Token has expired")).toString();
byte[] bytes = responseDataStr.getBytes(StandardCharsets.UTF_8);
DataBuffer buffer = response.bufferFactory().wrap(bytes);
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.writeWith(Mono.just(buffer));
}
信息组装。
1、返回ResponseData对象,输出状态码和状态信息:The Token has expired
ResponseData.error(HttpStatus.UNAUTHORIZED.value(), "The Token has expired")
转换成json信息
byte[] bytes = responseDataStr.getBytes(StandardCharsets.UTF_8);
转换成字节流
输出:
response.writeWith(Mono.just(buffer));
公共返回体封装:ResponseData
public class ResponseData<T> implements Serializable {
/**
*
*/
private static final long serialVersionUID = 7098362967623367826L;
/**
* 响应状态码
*/
private Integer code;
/**
* 响应信息
*/
private String message;
/**
* 响应对象
*/
private T data;
/** 是否加密返回参数*/
private boolean encrypt;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static <T> ResponseData<T> success() {
return success(ResponseCode.SUCCESS);
}
public static <T> ResponseData<T> success(ResponseCode code) {
return success(code, null);
}
public static <T> ResponseData<T> success(T object) {
return success(ResponseCode.SUCCESS, object);
}
public static <T> ResponseData<T> success(ResponseCode code, T object) {
return success(code.getCode(), code.getMessage(), object);
}
public static <T> ResponseData<T> success(Integer code, String message, T data) {
ResponseData<T> response = new ResponseData<>();
response.setCode(code);
response.setMessage(message);
response.setData(data);
return response;
}
public static <T> ResponseData<T> error() {
return error(ResponseCode.FAILED);
}
public static <T> ResponseData<T> error(ResponseCode code) {
return error(code, null);
}
public static <T> ResponseData<T> error(String message) {
return error(ResponseCode.FAILED.getCode(), message, null);
}
public static <T> ResponseData<T> error(Integer code, String message) {
return error(code, message, null);
}
public static <T> ResponseData<T> error(ResponseCode code, T data) {
return error(code.getCode(), code.getMessage(), data);
}
public static <T> ResponseData<T> error(Integer code, String message, T data) {
ResponseData<T> response = new ResponseData<>();
response.setCode(code);
response.setMessage(message);
response.setData(data);
return response;
}
public boolean isEncrypt() {
return encrypt;
}
public void setEncrypt(boolean encrypt) {
this.encrypt = encrypt;
}
}