首页 > 基础资料 博客日记
【开端】JAVA Mono<Void>向前端返回没有登陆或登录超时 暂无权限访问信息组装
2024-09-07 13:00:07基础资料围观98次
这篇文章介绍了【开端】JAVA Mono<Void>向前端返回没有登陆或登录超时 暂无权限访问信息组装,分享给大家做个参考,收藏Java资料网收获更多编程知识
一、绪论
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;
}
}
文章来源:https://blog.csdn.net/dongjing991/article/details/140986750
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: