作者:AlbertWen
添加时间:2021-12-10 19:56:35
修改时间:2025-11-14 14:17:38
分类:
07.Java框架/系统
...
编辑
【版本一】【推荐】
package com.fuyo.common.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fuyo.common.helper.JsonHelper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serial;
import java.io.Serializable;
/**
* @author Albert
* @since 2025-10-29
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
@Serial
private static final long serialVersionUID = -5869656105436411993L;
public static final String CODE_OK = "200";
private static final String CODE_ERROR = "500";
private static final String MSG_OK = "操作成功";
private static final String MSG_ERROR = "操作失败";
private String code;
private String msg;
private T data;
public static <T> Result<T> ok() {
return new Result<>(CODE_OK, Result.MSG_OK, null);
}
public static <T> Result<T> ok(String msg) {
return new Result<>(CODE_OK, msg, null);
}
public static <T> Result<T> ok(T data) {
return ok(CODE_OK, Result.MSG_OK, data);
}
public static <T> Result<T> ok(String msg, T data) {
return new Result<>(CODE_OK, msg, data);
}
public static <T> Result<T> ok(String code, String msg, T data) {
return new Result<>(code, msg, data);
}
public static <T> Result<T> error() {
return new Result<>(CODE_ERROR, MSG_ERROR, null);
}
public static <T> Result<T> error(String msg) {
return new Result<>(CODE_ERROR, msg, null);
}
public static <T> Result<T> error(T data) {
return new Result<>(CODE_ERROR, MSG_ERROR, data);
}
public static <T> Result<T> error(String msg, T data) {
return new Result<>(CODE_ERROR, msg, data);
}
public static <T> Result<T> error(String code, String msg, T data) {
return new Result<>(code, msg, data);
}
/**
* 结果是否Ok
* 添加 @JsonIgnore 注解,表示转JSON字符串时,忽略此方法
* 如果不忽略,就会返回 ok 字段
*/
@JsonIgnore
public boolean isOk() {
return CODE_OK.equals(code);
}
/**
* 结果是否Error
* 添加 @JsonIgnore 注解,表示转JSON字符串时,忽略此方法
* 如果不忽略,就会返回 error 字段
*/
@JsonIgnore
public boolean isError() {
return !isOk();
}
/**
* 输出Json字符串
* 添加 @JsonIgnore 注解,表示转JSON字符串时,忽略此方法
* 如果不忽略,就会返回 jsonStr 字段
*/
@JsonIgnore
public String toJsonStr() {
return JsonHelper.objectToString(data);
}
}
package com.fuyo.common.helper;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
/**
* Json 助手类
*
* @author AlbertWen
* @since 2025-10-29
*/
@Slf4j
public class JsonHelper {
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* 对象转字符串
*/
public static String objectToString(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
return "";
}
}
/**
* 字符串转对象
*/
public static <T> T stringToObject(String json, Class<T> object) {
try {
return objectMapper.readValue(json, object);
} catch (JsonProcessingException e) {
log.error(e.getMessage(), e);
return null;
}
}
/**
* 转JSON数组对象
*/
public static JSONArray parseArray(Object arrayOrCollection) {
JSONConfig config = JSONConfig.create().setIgnoreCase(false)
.setNatureKeyComparator().setDateFormat("yyyy-MM-dd HH:mm:ss");
return JSONUtil.parseArray(arrayOrCollection, config);
}
/**
* 转JSON对象
*/
public static JSONObject parseObject(Object obj) {
JSONConfig config = JSONConfig.create().setIgnoreCase(false)
.setNatureKeyComparator().setDateFormat("yyyy-MM-dd HH:mm:ss");
return JSONUtil.parseObj(obj, config);
}
/**
* List转JSON字符串
*/
public static String listToJsonStr(Object arrayOrCollection) {
return parseArray(arrayOrCollection).toString();
}
/**
* Object转JSON字符串
*/
public static String ObjectToJsonStr(Object obj) {
return parseObject(obj).toString();
}
}
【版本二】链式版
package com.wanma.helper;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 业务操作结果类
*/
public class Result<T> {
public final static String CODE_SUCCESS = "0"; // 成功状态码
public final static String CODE_FAIL = "000001"; //(默认的)失败状态码(前3位是模块名,后3位是消息码)
public final static String MSG_SUCCESS = "操作成功"; //(默认的)成功提示语
public final static String MSG_FAIL = "操作失败"; //(默认的)失败提示语
public final static String WIN_SELF = "window";
public final static String WIN_PARENT = "parent";
public final static String WIN_TOP = "top";
private String code = Result.CODE_FAIL; // 状态码,"0":成功,非"0":失败
private String msg = Result.MSG_FAIL; // 消息语
private T data = null; // 数据
private String url = ""; // 跳转URL
private int time = 1; // 跳转时间(单位:秒)
private String js = ""; // 执行的JS脚步
private boolean reload = false; // 是否刷新页面
private String window = "window"; // 刷新的窗体,可选项值:window|parent|top
private boolean dialog = false; // 是否显示对话框
/**
* 实例化一个对象
*/
public static <T> Result<T> instance() {
return new Result<T>();
}
public String getCode() {
return code;
}
public Result<T> setCode(String code) {
this.code = code;
return this;
}
public String getMsg() {
return msg;
}
public Result<T> setMsg(String msg) {
this.msg = msg;
return this;
}
public T getData() {
return data;
}
public Result<T> setData(T data) {
this.data = data;
return this;
}
public String getUrl() {
return url;
}
public Result<T> setUrl(String url) {
this.url = url;
return this;
}
public int getTime() {
return time;
}
public Result<T> setTime(int time) {
this.time = time;
return this;
}
public String getJs() {
return js;
}
public Result<T> setJs(String js) {
this.js = js;
return this;
}
public boolean isReload() {
return reload;
}
public Result<T> setReload(boolean reload) {
this.reload = reload;
return this;
}
public String getWindow() {
return window;
}
public Result<T> setWindow(String window) {
this.window = window;
return this;
}
public boolean isDialog() {
return dialog;
}
public Result<T> setDialog(boolean dialog) {
this.dialog = dialog;
return this;
}
public Result<T> setDialog() {
this.dialog = true;
return this;
}
public Result<T> success() {
code = Result.CODE_SUCCESS;
if (msg.equals("") || msg.equals(Result.MSG_FAIL)) {
msg = Result.MSG_SUCCESS;
}
return this;
}
public Result<T> success(String msg) {
code = Result.CODE_SUCCESS;
this.msg = msg;
return this;
}
public Result<T> success(T data) {
code = Result.CODE_SUCCESS;
this.data = data;
if (msg.equals("") || msg.equals(Result.MSG_FAIL)) {
msg = Result.MSG_SUCCESS;
}
return this;
}
public Result<T> success(String msg, T data) {
code = Result.CODE_SUCCESS;
this.msg = msg;
this.data = data;
return this;
}
public Result<T> fail() {
code = Result.CODE_FAIL;
if (msg.equals("") || msg.equals(Result.MSG_OK )) {
msg = Result.MSG_FAIL;
}
return this;
}
public Result<T> fail(String msg) {
code = Result.CODE_FAIL;
this.msg = msg;
return this;
}
public Result<T> fail(T data) {
code = Result.CODE_FAIL;
this.data = data;
if (msg.equals("") || msg.equals(Result.MSG_OK )) {
msg = Result.MSG_FAIL;
}
return this;
}
public Result<T> fail(String msg, T data) {
code = Result.CODE_FAIL;
this.msg = msg;
this.data = data;
return this;
}
/**
* 是否为 成功
*/
public boolean isSuccess() {
return code.equals(Result.CODE_SUCCESS);
}
/**
* 是否为 失败
*/
public boolean isFail() {
return !isSuccess();
}
@Override
public String toString() {
return "{\"code\":\"" + getCode() + "\",\"msg\":\"" + getMsg() + "\", \"data\": \"\"}";
}
public Map<String, Object> toMap() {
Map<String, Object> ret = new LinkedHashMap<>();
ret.put("code", code);
ret.put("msg", msg);
ret.put("data", data);
ret.put("url", url);
ret.put("time", time);
ret.put("js", js);
ret.put("reload", reload);
return ret;
}
/**
* 输出JSON字符串
*/
public String outJson() {
Map<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("code", code);
jsonMap.put("msg", msg);
jsonMap.put("data", data);
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(jsonMap);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "{\"code\":\"" + getCode() + "\",\"msg\":\"" + getMsg() + "\", \"data\": \"\"}";
}
/**
* 输出Ajax字符串
*/
public String outAjax() {
StringBuilder content = new StringBuilder();
if (!isDialog() && !getMsg().equals("")) {
content.append(getMsg());
}
String js = "";
if (isReload()) {
js = "<script type=\"text/javascript\" reload=\"1\">\n" +
" setTimeout(function(){\n" +
" #window#.location.reload();\n" +
" }, #time# * 1000);\n" +
"</script>";
js = js.replace("#window#", getWindow())
.replace("#time#", String.valueOf(getTime()));
content.append(js);
}
if (!isReload() && StrUtil.isNotEmpty(getUrl())) {
js = "<script type=\"text/javascript\" reload=\"1\">\n" +
" setTimeout(function(){\n" +
" #window#.location.href= \"#url#\";\n" +
" }, #time# * 1000);\n" +
"</script>";
js = js.replace("#window#", getWindow())
.replace("#time#", String.valueOf(getTime()))
.replace("#url#", getUrl());
content.append(js);
}
if (isDialog() && isSuccess()) {
if (getTime() < 2) {
setTime(2);
}
js = "<script type=\"text/javascript\" reload=\"1\">\n" +
" dialog_success('#msg#', #time#)\n" +
"</script>";
js = js.replace("#msg#", getMsg())
.replace("#time#", String.valueOf(getTime()));
content.append(js);
}
if (isDialog() && isFail()) {
if (getTime() < 3) {
setTime(3);
}
js = "<script type=\"text/javascript\" reload=\"1\">\n" +
" dialog_fail('#msg#', #time#})\n" +
"</script>";
js = js.replace("#msg#", getMsg())
.replace("#time#", String.valueOf(getTime()));
content.append(js);
}
if (StrUtil.isNotEmpty(getJs())) {
js = "<script type=\"text/javascript\" reload=\"1\">\n" +
" #js#\n" +
"</script>";
js = js.replace("#js#", getJs());
content.append(js);
}
return ResponseHelper.outXml(content.toString());
}
}