mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-08-05 18:29:31 +08:00
Compare commits
No commits in common. "41a9f1b507ab23ccd4aefb9efb73c9177bccb60f" and "c4fc4bd4f82cfe9abb930b874fb8d9190c2d77f7" have entirely different histories.
41a9f1b507
...
c4fc4bd4f8
@ -6,23 +6,11 @@ import com.fasterxml.jackson.core.JsonParseException;
|
|||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.*;
|
||||||
import com.fasterxml.jackson.databind.JavaType;
|
|
||||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
||||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
|
||||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||||
import com.fasterxml.jackson.databind.type.MapType;
|
import com.fasterxml.jackson.databind.type.MapType;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
import com.yexuejc.base.converter.LocalDateDeserializer;
|
import com.yexuejc.base.converter.*;
|
||||||
import com.yexuejc.base.converter.LocalDateSerializer;
|
|
||||||
import com.yexuejc.base.converter.LocalDateTimeDeserializer;
|
|
||||||
import com.yexuejc.base.converter.LocalDateTimeSerializer;
|
|
||||||
import com.yexuejc.base.converter.TimestampDeserializer;
|
|
||||||
import com.yexuejc.base.converter.TimestampSerializer;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -191,11 +179,11 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
pojo = jsonMapper.readValue(json, cls);
|
pojo = jsonMapper.readValue(json, cls);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + e);
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return pojo;
|
return pojo;
|
||||||
@ -213,13 +201,13 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
pojo = jsonMapper.readValue(json, cls);
|
pojo = jsonMapper.readValue(json, cls);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warning("json to Object Exception.\n" + StrUtil.printStackTrace(e));
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return pojo;
|
return pojo;
|
||||||
@ -239,11 +227,11 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
pojo = jsonMapper.readValue(json, javaType);
|
pojo = jsonMapper.readValue(json, javaType);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
return pojo;
|
return pojo;
|
||||||
}
|
}
|
||||||
@ -264,11 +252,11 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
pojo = jsonMapper.readValue(json, mapType);
|
pojo = jsonMapper.readValue(json, mapType);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
return pojo;
|
return pojo;
|
||||||
}
|
}
|
||||||
@ -285,7 +273,7 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
return jsonMapper.readValue(json, javaType);
|
return jsonMapper.readValue(json, javaType);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -305,11 +293,11 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
pojo = jsonMapper.readValue(json, javaType);
|
pojo = jsonMapper.readValue(json, javaType);
|
||||||
} catch (JsonParseException e) {
|
} catch (JsonParseException e) {
|
||||||
log.warning("json to Object JsonParseException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonParseException.\n" + e.getMessage());
|
||||||
} catch (JsonMappingException e) {
|
} catch (JsonMappingException e) {
|
||||||
log.warning("json to Object JsonMappingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonMappingException.\n" + e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.warning("json to Object IOException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object IOException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
return pojo;
|
return pojo;
|
||||||
}
|
}
|
||||||
@ -325,7 +313,7 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
json = jsonMapper.writeValueAsString(pojo);
|
json = jsonMapper.writeValueAsString(pojo);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.warning("json to Object JsonProcessingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@ -341,9 +329,8 @@ public class JsonUtil {
|
|||||||
try {
|
try {
|
||||||
json = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
json = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
log.warning("json to Object JsonProcessingException.\n" + StrUtil.printStackTrace(e));
|
log.warning("json to Object JsonProcessingException.\n" + e.getMessage());
|
||||||
}
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,20 +1,9 @@
|
|||||||
package com.yexuejc.base.util;
|
package com.yexuejc.base.util;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.http.HttpClient;
|
|
||||||
import java.net.http.HttpRequest;
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -30,7 +19,7 @@ public final class StrUtil {
|
|||||||
private StrUtil() {
|
private StrUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
public static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断字符串,数组,集合 是否为空
|
* 判断字符串,数组,集合 是否为空
|
||||||
@ -122,6 +111,40 @@ public final class StrUtil {
|
|||||||
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
|
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析aa=bb&cc=dd&ee=ff格式的字符串,返回HashMap
|
||||||
|
*
|
||||||
|
* @param urlencoded
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Map<String, String> parseUrlencoded(String urlencoded) {
|
||||||
|
if (isEmpty(urlencoded)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String[] entrys = urlencoded.split("&");
|
||||||
|
if (isEmpty(entrys)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, String> map = new HashMap<String, String>(16);
|
||||||
|
String[] kv = null;
|
||||||
|
for (String entry : entrys) {
|
||||||
|
if (isEmpty(entry)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
kv = entry.split("=");
|
||||||
|
if (isEmpty(kv)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (kv.length > 1) {
|
||||||
|
map.put(kv[0], kv[1]);
|
||||||
|
} else {
|
||||||
|
map.put(kv[0], null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字符串转换方法 把字节数组转换成16进制字符串
|
* 字符串转换方法 把字节数组转换成16进制字符串
|
||||||
*
|
*
|
||||||
@ -231,8 +254,8 @@ public final class StrUtil {
|
|||||||
/**
|
/**
|
||||||
* 对ID(32位)进行编码
|
* 对ID(32位)进行编码
|
||||||
*
|
*
|
||||||
* @param id 32位ID
|
* @param id
|
||||||
* @return 编码后的64位ID
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String codeId(String id) {
|
public static String codeId(String id) {
|
||||||
if (id == null || id.length() != 32) {
|
if (id == null || id.length() != 32) {
|
||||||
@ -259,8 +282,8 @@ public final class StrUtil {
|
|||||||
/**
|
/**
|
||||||
* 对ID(32位)进行解码
|
* 对ID(32位)进行解码
|
||||||
*
|
*
|
||||||
* @param coded 编码后的64位ID
|
* @param coded
|
||||||
* @return 解码后的32位ID
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String decodeId(String coded) {
|
public static String decodeId(String coded) {
|
||||||
if (coded == null || coded.length() != 64) {
|
if (coded == null || coded.length() != 64) {
|
||||||
@ -274,40 +297,6 @@ public final class StrUtil {
|
|||||||
return id.toString();
|
return id.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析aa=bb&cc=dd&ee=ff格式的字符串,返回HashMap
|
|
||||||
*
|
|
||||||
* @param urlencoded
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static Map<String, String> parseUrlencoded(String urlencoded) {
|
|
||||||
if (isEmpty(urlencoded)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String[] entrys = urlencoded.split("&");
|
|
||||||
if (isEmpty(entrys)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, String> map = new HashMap<String, String>(16);
|
|
||||||
String[] kv = null;
|
|
||||||
for (String entry : entrys) {
|
|
||||||
if (isEmpty(entry)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
kv = entry.split("=");
|
|
||||||
if (isEmpty(kv)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (kv.length > 1) {
|
|
||||||
map.put(kv[0], kv[1]);
|
|
||||||
} else {
|
|
||||||
map.put(kv[0], null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* map parameters 转url parameters
|
* map parameters 转url parameters
|
||||||
*
|
*
|
||||||
@ -381,7 +370,7 @@ public final class StrUtil {
|
|||||||
/**
|
/**
|
||||||
* 下划线字符
|
* 下划线字符
|
||||||
*/
|
*/
|
||||||
private static final char UNDERLINE = '_';
|
public static final char UNDERLINE = '_';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 字符串下划线转驼峰格式
|
* 字符串下划线转驼峰格式
|
||||||
@ -430,69 +419,4 @@ public final class StrUtil {
|
|||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String NEW_LINE = "\n";
|
|
||||||
private static final String ERROR_MESSAGE_FORMAT = "%s.%s(%s:%d)";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 把异常堆栈信息转化为字符串,同时把所有的errorMessage用\n方式拼接到printStackTrace前面 -> 替代 e.printStackTrace()
|
|
||||||
* <p>使用e.printStackTrace()不能作为字符串处理,本方法可以转换e.printStackTrace()为字符串</p>
|
|
||||||
* <p><b>printStackTrace</b>信息从<i>Caused by:</i>C开始</p>
|
|
||||||
*
|
|
||||||
* @param cause
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String printStackTraceAndMessage(Throwable cause) {
|
|
||||||
if (cause != null) {
|
|
||||||
StringBuilder msg = new StringBuilder();
|
|
||||||
if (isNotEmpty(cause.getMessage())) {
|
|
||||||
msg.append(cause.getMessage()).append(NEW_LINE);
|
|
||||||
}
|
|
||||||
String causedMsg = printStackTrace(cause, (eMessage) -> {
|
|
||||||
if (isNotEmpty(eMessage)) {
|
|
||||||
msg.append(eMessage).append(NEW_LINE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return msg.append(causedMsg).toString();
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 把异常堆栈信息转化为字符串 -> 替代 e.printStackTrace()
|
|
||||||
* <p>使用e.printStackTrace()不能作为字符串处理,本方法可以转换e.printStackTrace()为字符串</p>
|
|
||||||
*
|
|
||||||
* @param cause
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String printStackTrace(Throwable cause) {
|
|
||||||
return printStackTrace(cause, (eMessage) -> {
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String printStackTrace(Throwable cause, Consumer<String> consumer) {
|
|
||||||
if (cause != null) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
String cClass = cause.getClass().getName();
|
|
||||||
String eMessage = cause.getMessage();
|
|
||||||
StackTraceElement[] stackTrace = cause.getStackTrace();
|
|
||||||
Throwable caused = cause.getCause();
|
|
||||||
while (caused != null) {
|
|
||||||
cClass = caused.getClass().getName();
|
|
||||||
eMessage = caused.getMessage();
|
|
||||||
stackTrace = caused.getStackTrace();
|
|
||||||
caused = caused.getCause();
|
|
||||||
consumer.accept(eMessage);
|
|
||||||
}
|
|
||||||
sb.append("Caused by: ").append(cClass).append(": ").append(eMessage).append(NEW_LINE);
|
|
||||||
for (StackTraceElement element : stackTrace) {
|
|
||||||
sb.append("\tat ");
|
|
||||||
sb.append(String.format(ERROR_MESSAGE_FORMAT, element.getClassName(), element.getMethodName(),
|
|
||||||
element.getFileName(), element.getLineNumber()));
|
|
||||||
sb.append(NEW_LINE);
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user