You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package com.utils ;
import java.util.HashMap ;
import java.util.Map ;
//返回数据类, 继承自HashMap<String, Object>,
// 用于封装和返回业务处理的结果数据,通常用于前后端交互时返回响应信息。
public class R extends HashMap < String , Object > {
// 序列化版本号,用于确保不同版本的类在反序列化时的兼容性
private static final long serialVersionUID = 1L ;
//构造方法, 初始化一个R对象, 并设置默认的返回码为0( 通常表示成功) 。
public R ( ) {
put ( "code" , 0 ) ;
}
//静态方法, 返回一个表示错误的R对象, 错误码为500, 错误信息为“未知异常, 请联系管理员”。
// @return 表示错误的R对象。
public static R error ( ) {
return error ( 500 , "未知异常,请联系管理员" ) ;
}
// 静态方法, 返回一个表示错误的R对象, 错误码为500, 错误信息为传入的msg。
// @param msg 错误信息。
// @return 表示错误的R对象。
public static R error ( String msg ) {
return error ( 500 , msg ) ;
}
// 静态方法, 返回一个表示错误的R对象, 错误码为传入的code, 错误信息为传入的msg。
// @param code 错误码。
//@param msg 错误信息。
//@return 表示错误的R对象。
public static R error ( int code , String msg ) {
R r = new R ( ) ;
r . put ( "code" , code ) ;
r . put ( "msg" , msg ) ;
return r ;
}
// 静态方法, 返回一个表示成功的R对象, 包含传入的成功提示信息msg。
// @param msg 成功提示信息。
// @return 表示成功的R对象。
public static R ok ( String msg ) {
R r = new R ( ) ;
r . put ( "msg" , msg ) ;
return r ;
}
//静态方法, 返回一个表示成功的R对象, 将传入的map中的键值对添加到R对象中。
//@param map 包含数据的Map对象。
// @return 表示成功的R对象。
public static R ok ( Map < String , Object > map ) {
R r = new R ( ) ;
r . putAll ( map ) ;
return r ;
}
// 静态方法, 返回一个表示成功的R对象, 只包含默认的返回码0。
//@return 表示成功的R对象。
public static R ok ( ) {
return new R ( ) ;
}
//重写put方法, 将键值对添加到R对象中, 并返回当前R对象,
// 支持链式调用,方便连续添加多个键值对。
// @param key 键。
// @param value 值。
// @return 当前R对象。
public R put ( String key , Object value ) {
super . put ( key , value ) ;
return this ;
}
}