1、返回消息统一定义文件CodeMsg.java
- public class CodeMsg {
- private int retCode;
- private String message;
- // 按照模块定义CodeMsg
- // 通用异常
- public static CodeMsg SUCCESS = new CodeMsg(0,"success");
- public static CodeMsg SERVER_EXCEPTION = new CodeMsg(500100,"服务端异常");
- public static CodeMsg PARAMETER_ISNULL = new CodeMsg(500101,"输入参数为空");
- // 业务异常
- public static CodeMsg USER_NOT_EXSIST = new CodeMsg(500102,"用户不存在");
- public static CodeMsg ONLINE_USER_OVER = new CodeMsg(500103,"在线用户数超出允许登录的最大用户限制。");
- public static CodeMsg SESSION_NOT_EXSIST = new CodeMsg(500104,"不存在离线session数据");
- public static CodeMsg NOT_FIND_DATA = new CodeMsg(500105,"查找不到对应数据");
- private CodeMsg(int retCode, String message) {
- this.retCode = retCode;
- this.message = message;
- }
- public int getRetCode() {
- return retCode;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- }
public class CodeMsg {
private int retCode;
private String message;
// 按照模块定义CodeMsg
// 通用异常
public static CodeMsg SUCCESS = new CodeMsg(0,"success");
public static CodeMsg SERVER_EXCEPTION = new CodeMsg(500100,"服务端异常");
public static CodeMsg PARAMETER_ISNULL = new CodeMsg(500101,"输入参数为空");
// 业务异常
public static CodeMsg USER_NOT_EXSIST = new CodeMsg(500102,"用户不存在");
public static CodeMsg ONLINE_USER_OVER = new CodeMsg(500103,"在线用户数超出允许登录的最大用户限制。");
public static CodeMsg SESSION_NOT_EXSIST = new CodeMsg(500104,"不存在离线session数据");
public static CodeMsg NOT_FIND_DATA = new CodeMsg(500105,"查找不到对应数据");private CodeMsg(int retCode, String message) {
this.retCode = retCode;
this.message = message;
}
public int getRetCode() {
return retCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2、返回结果集封装:Result.java
- public class Result<T> {
- private String message;
- private int retCode;
- private T data;
- private Result(T data) {
- this.retCode = 0;
- this.message = "成功";
- this.data = data;
- }
- private Result(CodeMsg cm) {
- if(cm == null){
- return;
- }
- this.retCode = cm.getRetCode();
- this.message = cm.getMessage();
- }
- /**
- * 成功时候的调用
- * @return
- */
- public static <T> Result<T> success(T data){
- return new Result<T>(data);
- }
- /**
- * 成功,不需要传入参数
- * @return
- */
- @SuppressWarnings("unchecked")
- public static <T> Result<T> success(){
- return (Result<T>) success("");
- }
- /**
- * 失败时候的调用
- * @return
- */
- public static <T> Result<T> error(CodeMsg cm){
- return new Result<T>(cm);
- }
- /**
- * 失败时候的调用,扩展消息参数
- * @param cm
- * @param msg
- * @return
- */
- public static <T> Result<T> error(CodeMsg cm,String msg){
- cm.setMessage(cm.getMessage()+"--"+msg);
- return new Result<T>(cm);
- }
- public T getData() {
- return data;
- }
- public String getMessage() {
- return message;
- }
- public int getRetCode() {
- return retCode;
- }
- }
public class Result<T> {
private String message;
private int retCode;
private T data;
private Result(T data) {
this.retCode = 0;
this.message = "成功";
this.data = data;
}
private Result(CodeMsg cm) {
if(cm == null){
return;
}
this.retCode = cm.getRetCode();
this.message = cm.getMessage();
}
/**
* 成功时候的调用
* @return
*/
public static <T> Result<T> success(T data){
return new Result<T>(data);
}
/**
* 成功,不需要传入参数
* @return
*/
@SuppressWarnings("unchecked")
public static <T> Result<T> success(){
return (Result<T>) success("");
}
/**
* 失败时候的调用
* @return
*/
public static <T> Result<T> error(CodeMsg cm){
return new Result<T>(cm);
}
/**
* 失败时候的调用,扩展消息参数
* @param cm
* @param msg
* @return
*/
public static <T> Result<T> error(CodeMsg cm,String msg){
cm.setMessage(cm.getMessage()+"--"+msg);
return new Result<T>(cm);
}
public T getData() {
return data;
}
public String getMessage() {
return message;
}
public int getRetCode() {
return retCode;
}
}
3、返回结果使用:
- Map<String, Object> dataMap = new HashMap<String, Object>();
- dataMap.put("data", data);
- dataMap.put("userid", userid);
- result = Result.success(dataMap);// 成功,并返回数据和retCode和message
- // result = Result.success();// 成功,不返回数据,只返回retCode和message
- // result = Result.error(CodeMsg.SERVER_EXCEPTION);// 失败返回错误信息
- // result = Result.error(CodeMsg.SERVER_EXCEPTION,e.toString());// 失败返回错误+扩展错误信息
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("data", data);
dataMap.put("userid", userid); result = Result.success(dataMap);// 成功,并返回数据和retCode和message // result = Result.success();// 成功,不返回数据,只返回retCode和message // result = Result.error(CodeMsg.SERVER_EXCEPTION);// 失败返回错误信息 // result = Result.error(CodeMsg.SERVER_EXCEPTION,e.toString());// 失败返回错误+扩展错误信息
2018-05-15更新
日常开发中,将系统的返回msg可以封装成枚举(Emun)类型来处理。