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.
468 lines
15 KiB
468 lines
15 KiB
package com.ynxbd.common.result;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.annotation.JSONField;
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
import com.ynxbd.common.helper.common.ErrorHelper;
|
|
import com.ynxbd.common.helper.common.JsonHelper;
|
|
import lombok.*;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.http.HttpStatus;
|
|
import org.dom4j.Attribute;
|
|
import org.dom4j.Document;
|
|
import org.dom4j.DocumentHelper;
|
|
import org.dom4j.Element;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
@Slf4j
|
|
@Getter
|
|
@Setter(AccessLevel.PRIVATE)
|
|
@ToString
|
|
public class JsonResult {
|
|
public String code;
|
|
private String message;
|
|
// 数据
|
|
private Map<String, Object> dataMap;
|
|
// 成功编码--判断请求是否成功
|
|
private String successCode;
|
|
// 超时/错误编码
|
|
private String timeoutCode;
|
|
|
|
@FunctionalInterface
|
|
public interface MapParams {
|
|
void setParams(Map<String, Object> map);
|
|
}
|
|
|
|
private JsonResult() {
|
|
}
|
|
|
|
/**
|
|
* 请求成功
|
|
*/
|
|
@JsonIgnore
|
|
@JSONField(serialize = false)
|
|
public boolean success() {
|
|
return this.code.equals(this.successCode);
|
|
}
|
|
|
|
|
|
/**
|
|
* 请求成功
|
|
*/
|
|
@JsonIgnore
|
|
@JSONField(serialize = false)
|
|
public boolean isTimeout() {
|
|
return this.code.equals(this.timeoutCode);
|
|
}
|
|
|
|
/**
|
|
* 创建一个异常信息
|
|
*/
|
|
public static JsonResult createErrorResult(JsonResultEnum jsonResultEnum) {
|
|
return createResult(jsonResultEnum.getERROR_CODE(), String.format("【%s】调用失败", jsonResultEnum.getSYS()), jsonResultEnum);
|
|
}
|
|
|
|
/**
|
|
* 创建一个异常信息
|
|
*/
|
|
public static JsonResult createErrorResult(String message, JsonResultEnum jsonResultEnum) {
|
|
return createResult(jsonResultEnum.getERROR_CODE(), message, jsonResultEnum);
|
|
}
|
|
|
|
/**
|
|
* 创建一个超时信息
|
|
*/
|
|
public static JsonResult createTimeoutResult(String message, JsonResultEnum jsonResultEnum) {
|
|
return createResult(jsonResultEnum.getTIMEOUT_CODE(), message, jsonResultEnum);
|
|
}
|
|
|
|
/**
|
|
* 创建一个异常信息
|
|
*/
|
|
public static JsonResult createResult(@NonNull String code, @NonNull String message, JsonResultEnum jsonResultEnum) {
|
|
JsonResult result = new JsonResult();
|
|
result.code = code;
|
|
result.message = message;
|
|
result.successCode = jsonResultEnum.getSUCCESS_CODE();
|
|
if (jsonResultEnum.getTIMEOUT_CODE() == null) {
|
|
result.timeoutCode = String.valueOf(HttpStatus.SC_REQUEST_TIMEOUT);
|
|
} else {
|
|
result.timeoutCode = jsonResultEnum.getTIMEOUT_CODE();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 请求成功
|
|
*/
|
|
public static JsonResult jsonToBean(String respData, JsonResultEnum jsonResultEnum) {
|
|
if (respData == null) {
|
|
return null;
|
|
}
|
|
return mapToResult(JsonHelper.parseObject(respData), jsonResultEnum);
|
|
}
|
|
|
|
|
|
/**
|
|
* xml数据转对象
|
|
*
|
|
* @param xml xml数据
|
|
* @param ignoreParams 需要忽略的字段(可选)
|
|
* @return JsonResult
|
|
*/
|
|
public static JsonResult xmlToBean(String xml, JsonResultEnum jsonResultEnum, String... ignoreParams) {
|
|
try {
|
|
if (xml == null || "".equals(xml)) {
|
|
log.info("【{}】 response xml is null", jsonResultEnum.getSYS());
|
|
return null; // 不能判断是否为调用超时
|
|
}
|
|
|
|
Document document = DocumentHelper.parseText(xml);
|
|
Element root = document.getRootElement(); // 获取根节点元素对象
|
|
|
|
JSONObject respObj = nodeToJsonObject(root);
|
|
return mapToResult(respObj, jsonResultEnum, ignoreParams);
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static JsonResult mapToResult(JSONObject respObj, JsonResultEnum jsonResultEnum, String... ignoreParams) {
|
|
JsonResult result = new JsonResult();
|
|
Map<String, Object> map = new HashMap<>();
|
|
Set<String> keys = respObj.keySet();
|
|
for (String key : keys) {
|
|
if (ignoreParams != null && ignoreParams.length > 0) {
|
|
for (String ignoreParam : ignoreParams) {
|
|
if (key.equals(ignoreParam)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (jsonResultEnum.getCODE_NODE().equals(key)) {
|
|
result.code = respObj.getString(key);
|
|
continue;
|
|
}
|
|
if (jsonResultEnum.getMESSAGE_NODE().equals(key)) {
|
|
result.message = respObj.getString(key);
|
|
continue;
|
|
}
|
|
map.put(key, respObj.getString(key));
|
|
}
|
|
result.setDataMap(map);
|
|
result.dataMap = map;
|
|
result.successCode = jsonResultEnum.getSUCCESS_CODE();
|
|
if (jsonResultEnum.getTIMEOUT_CODE() == null) {
|
|
result.timeoutCode = String.valueOf(HttpStatus.SC_REQUEST_TIMEOUT);
|
|
} else {
|
|
result.timeoutCode = jsonResultEnum.getTIMEOUT_CODE();
|
|
}
|
|
if (!result.success()) {
|
|
log.info("{} Error Message:[{}]", jsonResultEnum.getSYS(), result.message);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 递归-Element转JSONObject
|
|
*
|
|
* @param rootNode 根节点
|
|
* @return JSONObject
|
|
*/
|
|
public static JSONObject nodeToJsonObject(Element rootNode) {
|
|
JSONObject result = new JSONObject();
|
|
// 当前节点的名称、文本内容和属性
|
|
for (Attribute o : rootNode.attributes()) {// 遍历当前节点的所有属性
|
|
result.put(o.getName(), o.getValue());
|
|
}
|
|
|
|
// 递归遍历当前节点所有的子节点
|
|
List<Element> listElement = rootNode.elements();// 所有一级子节点的list
|
|
if (!listElement.isEmpty()) {
|
|
String name, content;
|
|
for (Element e : listElement) {// 遍历所有一级子节点
|
|
name = e.getName();
|
|
content = e.getTextTrim();
|
|
if (e.attributes().isEmpty() && e.elements().isEmpty()) // 判断一级节点是否有属性和子节点
|
|
result.put(name, "".equals(content) ? null : content); // 沒有则将当前节点作为上级节点的属性对待
|
|
else {
|
|
if (!result.containsKey(e.getName())) { // 判断父节点是否存在该一级节点名称的属性
|
|
result.put(name, new JSONArray());// 没有则创建
|
|
}
|
|
JSONArray jsonArray = (JSONArray) result.get(name);
|
|
jsonArray.add(nodeToJsonObject(e)); // 将该一级节点放入该节点名称的属性对应的值中
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取DataMap的值 String
|
|
*
|
|
* @param nodeName 节点名称
|
|
* @return nodeVal 节点值
|
|
*/
|
|
public String getDataMapString(String nodeName) {
|
|
try {
|
|
Object val = this.dataMap.get(nodeName);
|
|
return val == null ? null : val.toString().trim();
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取DataMap的值 BigDecimal
|
|
*
|
|
* @param nodeName 节点名称
|
|
* @return nodeVal 节点值
|
|
*/
|
|
public BigDecimal getDataMapBigDecimal(String nodeName) {
|
|
try {
|
|
Object val = this.dataMap.get(nodeName);
|
|
return val == null ? null : new BigDecimal(val.toString());
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取DataMap的值 Integer
|
|
*
|
|
* @param nodeName 节点名称
|
|
* @return nodeVal 节点值
|
|
*/
|
|
public Integer getDataMapInteger(String nodeName) {
|
|
try {
|
|
Object val = this.dataMap.get(nodeName);
|
|
return val == null ? null : Integer.parseInt(val.toString());
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取DataMap的值 Boolean
|
|
*
|
|
* @param nodeName 节点名称
|
|
* @return nodeVal 节点值
|
|
*/
|
|
public Boolean getDataMapBoolean(String nodeName) {
|
|
try {
|
|
Object val = this.dataMap.get(nodeName);
|
|
return val == null ? null : Boolean.parseBoolean(val.toString());
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取DataMap的值 Long
|
|
*
|
|
* @param nodeName 节点名称
|
|
* @return nodeVal 节点值
|
|
*/
|
|
public Long getDataMapLong(String nodeName) {
|
|
try {
|
|
Object val = this.dataMap.get(nodeName);
|
|
return val == null ? null : Long.parseLong(val.toString());
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取DataMap的值 Double
|
|
*
|
|
* @param nodeName 节点名称
|
|
* @return nodeVal 节点值
|
|
*/
|
|
public Double getDataMapDouble(String nodeName) {
|
|
try {
|
|
Object val = this.dataMap.get(nodeName);
|
|
return val == null ? null : Double.parseDouble(val.toString());
|
|
} catch (Exception e) {
|
|
ErrorHelper.println(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取根节点包含相同节点数据
|
|
* 如:
|
|
* <roots>
|
|
* # <root>1</root>
|
|
* # <root>2</root>
|
|
* </roots>
|
|
*
|
|
* @param clazz 类型
|
|
* @param rootNode 根节点名
|
|
* @param nodes 子节点名,可变长参数
|
|
* @return 集合
|
|
*/
|
|
public <T> List<T> getDataMapList(Class<T> clazz, String rootNode, String... nodes) {
|
|
JSONArray jsonArray = getJsonArray(rootNode, nodes);
|
|
return JsonHelper.decodeList(jsonArray.toString(), clazz);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取嵌套多层的JSONArray
|
|
*
|
|
* @param rooNode 根节点
|
|
* @param nodes 子节点,可变长参数
|
|
* @return json数组
|
|
*/
|
|
public JSONArray getJsonArray(String rooNode, String... nodes) {
|
|
Object rootObj = this.dataMap.get(rooNode);
|
|
String result = rootObj == null ? JsonHelper.toJsonString(new JSONArray()) : rootObj.toString();
|
|
try {
|
|
JSONArray jsonArray;
|
|
JSONObject jsonObject;
|
|
for (String node : nodes) {
|
|
jsonArray = JsonHelper.parseArray(result);
|
|
if (jsonArray == null || jsonArray.size() == 0) {
|
|
return new JSONArray();
|
|
}
|
|
if (jsonArray.size() == 1) { // 如果只有一个值继续处理
|
|
jsonObject = jsonArray.getJSONObject(0);
|
|
result = JsonHelper.toJsonString(jsonObject.getJSONArray(node));
|
|
} else { // 如果有多个值返回当前数组
|
|
result = JsonHelper.toJsonString(jsonArray);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("JSON数据转换失败");
|
|
ErrorHelper.println(e);
|
|
}
|
|
return JSONArray.parseArray(result);
|
|
}
|
|
|
|
|
|
/**
|
|
* JSON树形结构数据向下JSONArray中传递
|
|
*
|
|
* @param jsonArray 根节点
|
|
* @param properties 节点属性,可变长参数
|
|
* @return JSONArray
|
|
*/
|
|
public <T> List<T> jsonParentToChild(Class<T> clazz, JSONArray jsonArray, String rootNode, String... properties) {
|
|
JSONArray result = new JSONArray();
|
|
JSONObject jsonObject, childNode;
|
|
JSONArray nodeJsonArr;
|
|
for (int i = 0; i < jsonArray.size(); i++) {
|
|
jsonObject = jsonArray.getJSONObject(i);
|
|
nodeJsonArr = jsonObject.getJSONArray(rootNode);
|
|
for (int j = 0; j < nodeJsonArr.size(); j++) {
|
|
childNode = nodeJsonArr.getJSONObject(j);
|
|
for (String property : properties) {
|
|
for (String key : jsonObject.keySet()) { // 忽略大小写
|
|
if (key.equalsIgnoreCase(property)) {
|
|
property = key;
|
|
break;
|
|
}
|
|
}
|
|
String propertyVal = jsonObject.getString(property);
|
|
childNode.put(property, propertyVal);
|
|
}
|
|
result.add(childNode);
|
|
}
|
|
}
|
|
return JsonHelper.decodeList(JsonHelper.toJsonString(result), clazz);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取DataMap为bean
|
|
*
|
|
* @param clazz 类型
|
|
* @return bean
|
|
*/
|
|
public <T> T dataMapToBean(Class<T> clazz) {
|
|
return JsonHelper.decodeBean(JsonHelper.toJsonString(this.dataMap), clazz);
|
|
}
|
|
|
|
/**
|
|
* 获取DataMap为bean
|
|
*
|
|
* @param clazz 类型
|
|
* @return bean
|
|
*/
|
|
public <T> T dataMapToBean(Class<T> clazz, String rooNode, String... nodes) {
|
|
JSONObject jsonObject = getJsonObject(rooNode, nodes);
|
|
if (jsonObject == null) {
|
|
jsonObject = new JSONObject();
|
|
}
|
|
return JsonHelper.decodeBean(jsonObject, clazz);
|
|
}
|
|
|
|
// /**
|
|
// * 获取DataMap为bean
|
|
// *
|
|
// * @param rooNode 根节点
|
|
// * @param nodes 子节点
|
|
// * @return bean
|
|
// */
|
|
// public JSONObject getDataMapObject(String rooNode, String... nodes) {
|
|
// JSONObject jsonObject = getJsonObject(rooNode, nodes);
|
|
// if (jsonObject == null) {
|
|
// jsonObject = new JSONObject();
|
|
// }
|
|
// return decodeBean(jsonObject.toJSONString());
|
|
// }
|
|
|
|
|
|
/**
|
|
* 获取嵌套多层的JSONArray
|
|
*
|
|
* @param rooNode 根节点
|
|
* @param nodes 子节点,可变长参数
|
|
* @return json对象
|
|
*/
|
|
public JSONObject getJsonObject(String rooNode, String... nodes) {
|
|
Object rootObj = this.dataMap.get(rooNode);
|
|
String result = rootObj == null ? new JSONObject().toJSONString() : rootObj.toString();
|
|
try {
|
|
JSONArray jsonArray = JSONArray.parseArray(result);
|
|
if (jsonArray == null || jsonArray.size() == 0) {
|
|
return new JSONObject();
|
|
}
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(0);
|
|
if (jsonObject == null) {
|
|
return new JSONObject();
|
|
}
|
|
|
|
if (nodes.length == 0) {
|
|
result = jsonObject.toJSONString();
|
|
}
|
|
|
|
for (String node : nodes) {
|
|
result = jsonObject.getJSONObject(node).toJSONString();
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("JSON数据转换失败");
|
|
ErrorHelper.println(e);
|
|
}
|
|
return JsonHelper.parseObject(result);
|
|
}
|
|
|
|
}
|
|
|