Java tutorial
/* * Copyright 2015-2020 Fengduo.com All right reserved. This software is the confidential and proprietary information of * Fengduo.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with Fengduo.com. */ package com.fengduo.bee.commons.result; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.springframework.web.context.request.async.DeferredResult; import com.fengduo.bee.commons.cons.ResultCode; import com.fengduo.bee.commons.core.lang.Argument; import com.google.gson.Gson; /** * AjaxJSON * * @author zxc May 28, 2015 10:58:33 PM */ public class JsonResultUtils { public static JsonResult build(ResultCode code, String message) { return buildJsonResult(null, code, null, message, true); } public static JsonResult success() { return success(null, null); } public static JsonResult success(Object data) { return success(data, null); } public static JsonResult success(Object data, boolean escape) { return success(data, null, escape); } public static JsonResult success(Object data, String message) { return buildJsonResult(ResultCode.SUCCESS, data, message); } public static JsonResult success(Object data, String message, boolean escape) { return buildJsonResult(null, ResultCode.SUCCESS, data, message, escape); } public static JsonResult needLoJsonResult() { return needLogin(null, null); } public static JsonResult needLogin(Object data) { return needLogin(data, null); } public static JsonResult needLogin(Object data, String message) { return buildJsonResult(ResultCode.NEED_LOGIN, data, message); } public static JsonResult error() { return error(null, null); } public static JsonResult error(String message) { return error(null, message); } public static JsonResult error(String message, boolean escape) { return error(null, message, escape); } public static JsonResult error(Map<String, ? extends Object> data) { return error(data, null); } public static JsonResult error(Map<String, ? extends Object> data, boolean escape) { return error(data, null); } public static JsonResult error(Object data, String message) { return buildJsonResult(ResultCode.ERROR, data, message); } public static JsonResult error(Object data, String message, boolean escape) { return buildJsonResult(null, ResultCode.ERROR, data, message, escape); } public static JsonResult submitted(Object data) { return buildJsonResult(ResultCode.SUBMITED, data, null); } public static JsonResult forbidden() { return buildJsonResult(ResultCode.FORBIDDEN, null, null); } public static JsonResult forbidden(Object data, String message) { return buildJsonResult(ResultCode.FORBIDDEN, data, message); } public static JsonResult forbidden(Object data, String message, boolean escape) { return buildJsonResult(null, ResultCode.FORBIDDEN, data, message, escape); } public static String getSubmitedJson(String data) { String submitedJson = StringUtils.EMPTY; Map<String, Object> params = new HashMap<String, Object>(); params.put("result", ResultCode.SUBMITED.getValue()); params.put("message", getMessage(ResultCode.SUBMITED, null)); params.put("data", StringUtils.isNotEmpty(data) ? data : StringUtils.EMPTY); try { submitedJson = new Gson().toJson(params); } catch (Exception e) { } return submitedJson; } public static String getForbiddenJson() { String forbiddenJson = StringUtils.EMPTY; Map<String, Object> params = new HashMap<String, Object>(); params.put("result", ResultCode.FORBIDDEN.getValue()); params.put("message", getMessage(ResultCode.FORBIDDEN, null)); params.put("data", ""); try { forbiddenJson = new Gson().toJson(params); } catch (Exception e) { } return forbiddenJson; } public static String getNeedLoginJson() { String needLoginJson = StringUtils.EMPTY; Map<String, Object> params = new HashMap<String, Object>(); params.put("result", ResultCode.NEED_LOGIN.getValue());// ?? params.put("message", getMessage(ResultCode.NEED_LOGIN, null));// ??? params.put("data", "");// ?? try { needLoginJson = new Gson().toJson(params); } catch (Exception e) { } return needLoginJson; } public static String createJsonResult(ResultCode code, Object data, String message) { return new Gson().toJson(new DefaultJsonResult(code.getValue(), getMessage(code, message), data == null ? StringUtils.EMPTY : data)); } public static String createJsonResult(ResultCode code, String message) { return createJsonResult(code, null, message); } public static JsonResult buildJsonResult(ResultCode code, Object data, String message) { return buildJsonResult(null, code, data, message, true); } private static JsonResult buildJsonResult(String callback, ResultCode code, Object data, String message, boolean escape) { DefaultJsonResult jsonResult = new DefaultJsonResult(code.getValue(), getMessage(code, message), data == null ? StringUtils.EMPTY : data); JsonResult deferredResult = new JsonResult(); deferredResult.setResult(jsonResult); return deferredResult; } public static class JsonResult extends DeferredResult<DefaultJsonResult> { } static class DefaultJsonResult { private int code; private String message; private Object data; public DefaultJsonResult(int code, String message, Object data) { setCode(code); setMessage(message); setData(data); } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } } private static String getMessage(ResultCode code, String message) { if (Argument.isNotBlank(message)) { return message; } switch (code) { case SUCCESS: return "??"; case ERROR: return "?"; case NEED_LOGIN: return "?"; case SUBMITED: return "?????"; case FORBIDDEN: return "???"; default: return code.name(); } } public static <T> DeferredResult<IResult> deferredResult(GenericsResult<T> iResult) { DeferredResult<IResult> dr = new DeferredResult<IResult>(); dr.setResult(iResult); return dr; } }