[*] 每日优化
This commit is contained in:
@@ -20,7 +20,7 @@ import lombok.Setter;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Header implements Serializable {
|
||||
public class Header implements Cloneable, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.acgist.taoyao.boot.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@@ -62,43 +61,17 @@ public class Message implements Cloneable, Serializable {
|
||||
}
|
||||
|
||||
public Message(String code, String message, Header header, Object body) {
|
||||
this.code = code;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.header = header;
|
||||
this.body = body;
|
||||
this.header = header;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param code 状态编码
|
||||
*/
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messageCode 状态编码
|
||||
*/
|
||||
public void setCode(MessageCode messageCode) {
|
||||
this.setCode(messageCode, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messageCode 状态编码
|
||||
* @param message 状态描述
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public Message setCode(MessageCode messageCode, String message) {
|
||||
this.code = messageCode.getCode();
|
||||
this.message = StringUtils.isEmpty(message) ? messageCode.getMessage() : message;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 成功消息
|
||||
*/
|
||||
public static final Message success() {
|
||||
return success(null);
|
||||
return Message.success(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,7 +90,7 @@ public class Message implements Cloneable, Serializable {
|
||||
* @return 失败消息
|
||||
*/
|
||||
public static final Message fail() {
|
||||
return fail(null, null, null);
|
||||
return Message.fail(null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,7 +99,7 @@ public class Message implements Cloneable, Serializable {
|
||||
* @return 失败消息
|
||||
*/
|
||||
public static final Message fail(MessageCode messageCode) {
|
||||
return fail(messageCode, null, null);
|
||||
return Message.fail(messageCode, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,7 +109,7 @@ public class Message implements Cloneable, Serializable {
|
||||
* @return 失败消息
|
||||
*/
|
||||
public static final Message fail(MessageCode messageCode, Object body) {
|
||||
return fail(messageCode, null, body);
|
||||
return Message.fail(messageCode, null, body);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,7 +118,7 @@ public class Message implements Cloneable, Serializable {
|
||||
* @return 失败消息
|
||||
*/
|
||||
public static final Message fail(String message) {
|
||||
return fail(null, message, null);
|
||||
return Message.fail(null, message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,7 +128,7 @@ public class Message implements Cloneable, Serializable {
|
||||
* @return 失败消息
|
||||
*/
|
||||
public static final Message fail(String message, Object body) {
|
||||
return fail(null, message, body);
|
||||
return Message.fail(null, message, body);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +138,7 @@ public class Message implements Cloneable, Serializable {
|
||||
* @return 失败消息
|
||||
*/
|
||||
public static final Message fail(MessageCode messageCode, String message) {
|
||||
return fail(messageCode, message, null);
|
||||
return Message.fail(messageCode, message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,6 +159,25 @@ public class Message implements Cloneable, Serializable {
|
||||
public Message clone() {
|
||||
return new Message(this.code, this.message, this.header.clone(), this.body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messageCode 状态编码
|
||||
*/
|
||||
public void setCode(MessageCode messageCode) {
|
||||
this.setCode(messageCode, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messageCode 状态编码
|
||||
* @param message 状态描述
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public Message setCode(MessageCode messageCode, String message) {
|
||||
this.code = messageCode.getCode();
|
||||
this.message = StringUtils.isEmpty(message) ? messageCode.getMessage() : message;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆消息排除消息主体
|
||||
@@ -197,19 +189,25 @@ public class Message implements Cloneable, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Map消息主体
|
||||
* @return 消息主体
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public Map<String, Object> body() {
|
||||
if(this.body instanceof Map map) {
|
||||
return map;
|
||||
} else if(this.body == null) {
|
||||
return Map.of();
|
||||
} else {
|
||||
throw MessageCodeException.of("信令主体类型错误:" + this.body);
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T body() {
|
||||
return (T) this.body;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注解不会自动生成
|
||||
*
|
||||
* @param code 状态编码
|
||||
*/
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean isSuccess() {
|
||||
return CODE_0000.equals(this.code);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ public enum MessageCode {
|
||||
CODE_9999("9999", 500, "未知错误");
|
||||
|
||||
/**
|
||||
* HTTP状态编码前缀
|
||||
* HTTP Status前缀
|
||||
*/
|
||||
private static final String HTTP_STATUS = "3";
|
||||
|
||||
@@ -50,7 +50,7 @@ public enum MessageCode {
|
||||
*/
|
||||
private final String code;
|
||||
/**
|
||||
* 状态数值
|
||||
* 状态数值(HTTP Status)
|
||||
*/
|
||||
private final Integer status;
|
||||
/**
|
||||
@@ -59,8 +59,8 @@ public enum MessageCode {
|
||||
private final String message;
|
||||
|
||||
private MessageCode(String code, Integer status, String message) {
|
||||
this.code = code;
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
this.status = status;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public enum MessageCode {
|
||||
*/
|
||||
public static final MessageCode of(String code) {
|
||||
final MessageCode[] values = MessageCode.values();
|
||||
for (MessageCode value : values) {
|
||||
for (final MessageCode value : values) {
|
||||
if (value.code.equals(code)) {
|
||||
return value;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public enum MessageCode {
|
||||
* @return 状态编码
|
||||
*/
|
||||
public static final MessageCode of(Integer status) {
|
||||
return of(HTTP_STATUS + status);
|
||||
return MessageCode.of(HTTP_STATUS + status);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ import lombok.Getter;
|
||||
* @author acgist
|
||||
*/
|
||||
public final class DateUtils {
|
||||
|
||||
private DateUtils() {
|
||||
}
|
||||
|
||||
private DateUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期
|
||||
@@ -26,7 +26,7 @@ public final class DateUtils {
|
||||
* @author acgist
|
||||
*/
|
||||
@Getter
|
||||
public static enum DateStyle {
|
||||
public enum DateStyle {
|
||||
|
||||
YYMMDD("yyMMdd"),
|
||||
YYYYMMDD("yyyyMMdd"),
|
||||
@@ -48,56 +48,47 @@ public final class DateUtils {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间
|
||||
*
|
||||
* @author acgist
|
||||
*/
|
||||
@Getter
|
||||
public static enum TimeStyle {
|
||||
|
||||
/**
|
||||
* 时间
|
||||
*
|
||||
* @author acgist
|
||||
*/
|
||||
@Getter
|
||||
public enum TimeStyle {
|
||||
|
||||
HH24("HH"),
|
||||
HH24MM("HHmm"),
|
||||
HH24_MM("HH:mm"),
|
||||
HH24MMSS("HHmmss"),
|
||||
HH24_MM_SS("HH:mm:ss"),
|
||||
HH24MMSSSSS("HHmmssSSS"),
|
||||
HH24_MM_SS_SSS("HH:mm:ss.SSS");
|
||||
HH24("HH"),
|
||||
HH24MM("HHmm"),
|
||||
HH24_MM("HH:mm"),
|
||||
HH24MMSS("HHmmss"),
|
||||
HH24_MM_SS("HH:mm:ss"),
|
||||
HH24MMSSSSS("HHmmssSSS"),
|
||||
HH24_MM_SS_SSS("HH:mm:ss.SSS");
|
||||
|
||||
/**
|
||||
* 格式
|
||||
*/
|
||||
private final String format;
|
||||
/**
|
||||
* 格式工具
|
||||
*/
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
/**
|
||||
* 格式
|
||||
*/
|
||||
private final String format;
|
||||
/**
|
||||
* 格式工具
|
||||
*/
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
private TimeStyle(String format) {
|
||||
this.format = format;
|
||||
this.dateTimeFormatter = DateTimeFormatter.ofPattern(format);
|
||||
}
|
||||
private TimeStyle(String format) {
|
||||
this.format = format;
|
||||
this.dateTimeFormatter = DateTimeFormatter.ofPattern(format);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期时间
|
||||
*
|
||||
* @author acgist
|
||||
*/
|
||||
@Getter
|
||||
public static enum DateTimeStyle {
|
||||
public enum DateTimeStyle {
|
||||
|
||||
// YYYY
|
||||
YYYYMMDD_HH24_MM("yyyyMMdd HH:mm"),
|
||||
YYYY_MM_DD_HH24_MM("yyyy-MM-dd HH:mm"),
|
||||
YYYYMMDDHH24MMSS("yyyyMMddHHmmss"),
|
||||
YYYYMMDDHH24MMSSSSS("yyyyMMddHHmmssSSS"),
|
||||
YYYYMMDD_HH24_MM_SS("yyyyMMdd HH:mm:ss"),
|
||||
YYYYMMDD_HH24_MM_SS_SSS("yyyyMMdd HH:mm:ss.SSS"),
|
||||
YYYY_MM_DD_HH24_MM_SS("yyyy-MM-dd HH:mm:ss"),
|
||||
YYYY_MM_DD_HH24_MM_SS_SSS("yyyy-MM-dd HH:mm:ss.SSS"),
|
||||
// YY
|
||||
YYMMDD_HH24_MM("yyMMdd HH:mm"),
|
||||
YY_MM_DD_HH24_MM("yy-MM-dd HH:mm"),
|
||||
@@ -107,6 +98,15 @@ public final class DateUtils {
|
||||
YYMMDD_HH24_MM_SS_SSS("yyMMdd HH:mm:ss.SSS"),
|
||||
YY_MM_DD_HH24_MM_SS("yy-MM-dd HH:mm:ss"),
|
||||
YY_MM_DD_HH24_MM_SS_SSS("yy-MM-dd HH:mm:ss.SSS"),
|
||||
// YYYY
|
||||
YYYYMMDD_HH24_MM("yyyyMMdd HH:mm"),
|
||||
YYYY_MM_DD_HH24_MM("yyyy-MM-dd HH:mm"),
|
||||
YYYYMMDDHH24MMSS("yyyyMMddHHmmss"),
|
||||
YYYYMMDDHH24MMSSSSS("yyyyMMddHHmmssSSS"),
|
||||
YYYYMMDD_HH24_MM_SS("yyyyMMdd HH:mm:ss"),
|
||||
YYYYMMDD_HH24_MM_SS_SSS("yyyyMMdd HH:mm:ss.SSS"),
|
||||
YYYY_MM_DD_HH24_MM_SS("yyyy-MM-dd HH:mm:ss"),
|
||||
YYYY_MM_DD_HH24_MM_SS_SSS("yyyy-MM-dd HH:mm:ss.SSS"),
|
||||
// ISO
|
||||
YY_MM_DD_HH24_MM_SS_ISO("yy-MM-dd'T'HH:mm:ss"),
|
||||
YY_MM_DD_HH24_MM_SS_SSS_ISO("yy-MM-dd'T'HH:mm:ss.SSS"),
|
||||
@@ -134,109 +134,105 @@ public final class DateUtils {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成时间戳
|
||||
*
|
||||
* @return 时间戳
|
||||
*
|
||||
* @see #buildTime(LocalDateTime)
|
||||
*/
|
||||
public static final String buildTime() {
|
||||
return buildTime(LocalDateTime.now());
|
||||
}
|
||||
/**
|
||||
* @return 时间戳
|
||||
*
|
||||
* @see #buildTime(LocalDateTime)
|
||||
*/
|
||||
public static final String buildTime() {
|
||||
return DateUtils.buildTime(LocalDateTime.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成时间戳
|
||||
*
|
||||
* @param localDateTime 日期时间
|
||||
*
|
||||
* @return 时间戳
|
||||
*/
|
||||
public static final String buildTime(LocalDateTime localDateTime) {
|
||||
if (Objects.isNull(localDateTime)) {
|
||||
return buildTime();
|
||||
}
|
||||
return DateTimeStyle.YYYYMMDDHH24MMSS.getDateTimeFormatter().format(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转化
|
||||
*
|
||||
* @param date Date
|
||||
*
|
||||
* @return LocalDate
|
||||
*/
|
||||
public static final LocalDate toLocalDate(Date date) {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转化
|
||||
*
|
||||
* @param date Date
|
||||
*
|
||||
* @return LocalTime
|
||||
*/
|
||||
public static final LocalTime toLocalTime(Date date) {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
|
||||
}
|
||||
/**
|
||||
* @param localDateTime 日期时间
|
||||
*
|
||||
* @return 时间戳
|
||||
*/
|
||||
public static final String buildTime(LocalDateTime localDateTime) {
|
||||
if (Objects.isNull(localDateTime)) {
|
||||
return DateUtils.buildTime();
|
||||
}
|
||||
return DateTimeStyle.YYYYMMDDHH24MMSS.getDateTimeFormatter().format(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转化
|
||||
*
|
||||
* @param date Date
|
||||
*
|
||||
* @return LocalDate
|
||||
*/
|
||||
public static final LocalDate toLocalDate(Date date) {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转化
|
||||
*
|
||||
* @param date Date
|
||||
*
|
||||
* @return LocalTime
|
||||
*/
|
||||
public static final LocalTime toLocalTime(Date date) {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期转化
|
||||
*
|
||||
* @param date Date
|
||||
*
|
||||
* @return LocalDateTime
|
||||
*/
|
||||
public static final LocalDateTime toLocalDateTime(Date date) {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
}
|
||||
/**
|
||||
* 日期转化
|
||||
*
|
||||
* @param date Date
|
||||
*
|
||||
* @return LocalDateTime
|
||||
*/
|
||||
public static final LocalDateTime toLocalDateTime(Date date) {
|
||||
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换毫秒
|
||||
*
|
||||
* @param localDateTime LocalDateTime
|
||||
*
|
||||
* @return 毫秒
|
||||
*/
|
||||
public static final long toMilli(LocalDateTime localDateTime) {
|
||||
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
*
|
||||
* @param localDate LocalDate
|
||||
* @param format 格式
|
||||
*
|
||||
* @return 日期字符串
|
||||
*/
|
||||
public static String format(LocalDate localDate, DateStyle format) {
|
||||
return localDate != null && format != null ? format.getDateTimeFormatter().format(localDate) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param localTime LocalTime
|
||||
* @param format 格式
|
||||
*
|
||||
* @return 时间字符串
|
||||
*/
|
||||
public static String format(LocalTime localTime, TimeStyle format) {
|
||||
return localTime != null && format != null ? format.getDateTimeFormatter().format(localTime) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间
|
||||
*
|
||||
* @param localDateTime LocalDateTime
|
||||
* @param format 格式
|
||||
*
|
||||
* @return 日期时间字符串
|
||||
*/
|
||||
public static String format(LocalDateTime localDateTime, DateTimeStyle format) {
|
||||
return localDateTime != null && format != null ? format.getDateTimeFormatter().format(localDateTime) : null;
|
||||
}
|
||||
/**
|
||||
* 转换毫秒
|
||||
*
|
||||
* @param localDateTime LocalDateTime
|
||||
*
|
||||
* @return 毫秒
|
||||
*/
|
||||
public static final long toMilli(LocalDateTime localDateTime) {
|
||||
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
*
|
||||
* @param localDate LocalDate
|
||||
* @param format 格式
|
||||
*
|
||||
* @return 日期字符串
|
||||
*/
|
||||
public static String format(LocalDate localDate, DateStyle format) {
|
||||
return localDate != null && format != null ? format.getDateTimeFormatter().format(localDate) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
*
|
||||
* @param localTime LocalTime
|
||||
* @param format 格式
|
||||
*
|
||||
* @return 时间字符串
|
||||
*/
|
||||
public static String format(LocalTime localTime, TimeStyle format) {
|
||||
return localTime != null && format != null ? format.getDateTimeFormatter().format(localTime) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间
|
||||
*
|
||||
* @param localDateTime LocalDateTime
|
||||
* @param format 格式
|
||||
*
|
||||
* @return 日期时间字符串
|
||||
*/
|
||||
public static String format(LocalDateTime localDateTime, DateTimeStyle format) {
|
||||
return localDateTime != null && format != null ? format.getDateTimeFormatter().format(localDateTime) : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,9 +11,6 @@ import java.util.Objects;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.acgist.taoyao.boot.model.MessageCodeException;
|
||||
import com.acgist.taoyao.boot.utils.DateUtils.DateStyle;
|
||||
import com.acgist.taoyao.boot.utils.DateUtils.DateTimeStyle;
|
||||
import com.acgist.taoyao.boot.utils.DateUtils.TimeStyle;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -37,196 +34,190 @@ import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
|
||||
*/
|
||||
public final class JSONUtils {
|
||||
|
||||
private JSONUtils() {
|
||||
}
|
||||
/**
|
||||
* Mapper(线程安全)
|
||||
*/
|
||||
private static final ObjectMapper MAPPER = JSONUtils.buildMapper();
|
||||
|
||||
private JSONUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Java转JSON
|
||||
*
|
||||
* @param object Java
|
||||
*
|
||||
* @return JSON
|
||||
*/
|
||||
public static final String toJSON(Object object) {
|
||||
if (Objects.isNull(object)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw MessageCodeException.of(e, "Java转JSON失败:" + object);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Java
|
||||
*
|
||||
* @param <T> Java类型
|
||||
* @param json JSON
|
||||
*
|
||||
* @return Java
|
||||
*/
|
||||
public static final <T> T toJava(String json) {
|
||||
if (Objects.isNull(json)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<T>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Java失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapper(线程安全)
|
||||
*/
|
||||
private static final ObjectMapper MAPPER = buildMapper();
|
||||
|
||||
/**
|
||||
* Java转JSON
|
||||
*
|
||||
* @param object Java
|
||||
*
|
||||
* @return JSON
|
||||
*/
|
||||
public static final String toJSON(Object object) {
|
||||
if (Objects.isNull(object)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.writeValueAsString(object);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw MessageCodeException.of(e, "Java转JSON失败:" + object);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Java
|
||||
*
|
||||
* @param <T> Java类型
|
||||
*
|
||||
* @param json JSON
|
||||
*
|
||||
* @return Java
|
||||
*/
|
||||
public static final <T> T toJava(String json) {
|
||||
if (Objects.isNull(json)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<T>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Java失败:" + json);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* JSON转Java
|
||||
*
|
||||
* @param <T> Java类型
|
||||
* @param json JSON
|
||||
* @param clazz Java类型
|
||||
*
|
||||
* @return Java
|
||||
*/
|
||||
public static final <T> T toJava(String json, Class<T> clazz) {
|
||||
if (Objects.isNull(json) || Objects.isNull(clazz)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, clazz);
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Java失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Java
|
||||
*
|
||||
* @param <T> Java类型
|
||||
* @param json JSON
|
||||
* @param type Java类型
|
||||
*
|
||||
* @return Java
|
||||
*/
|
||||
public static final <T> T toJava(String json, TypeReference<T> type) {
|
||||
if (Objects.isNull(json) || Objects.isNull(type)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, type);
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Java失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Java
|
||||
*
|
||||
* @param <T> Java类型
|
||||
*
|
||||
* @param json JSON
|
||||
* @param clazz Java类型
|
||||
*
|
||||
* @return Java
|
||||
*/
|
||||
public static final <T> T toJava(String json, Class<T> clazz) {
|
||||
if (Objects.isNull(json) || Objects.isNull(clazz)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, clazz);
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Java失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Java
|
||||
*
|
||||
* @param <T> Java类型
|
||||
*
|
||||
* @param json JSON
|
||||
* @param type Java类型
|
||||
*
|
||||
* @return Java
|
||||
*/
|
||||
public static final <T> T toJava(String json, TypeReference<T> type) {
|
||||
if (Objects.isNull(json) || Objects.isNull(type)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, type);
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Java失败:" + json);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* JSON转Map
|
||||
*
|
||||
* @param <K> K类型
|
||||
* @param <V> V类型
|
||||
* @param json JSON
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public static final <K, V> Map<K, V> toMap(String json) {
|
||||
if (Objects.isNull(json)) {
|
||||
return Map.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<Map<K, V>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Map失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Map
|
||||
*
|
||||
* @param <K> K类型
|
||||
* @param <V> V类型
|
||||
*
|
||||
* @param json JSON
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public static final <K, V> Map<K, V> toMap(String json) {
|
||||
if (Objects.isNull(json)) {
|
||||
return Map.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<Map<K, V>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Map失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转List
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
*
|
||||
* @param json JSON
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public static final <T> List<T> toList(String json) {
|
||||
if (Objects.isNull(json)) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<List<T>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转List失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转List
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
*
|
||||
* @param json JSON
|
||||
* @param clazz 类型
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public static final <T> List<T> toList(String json, Class<T> clazz) {
|
||||
if (Objects.isNull(json)) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<List<T>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转List失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Mapper
|
||||
*/
|
||||
public static final ObjectMapper buildMapper() {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper
|
||||
.setTimeZone(TimeZone.getDefault())
|
||||
.setDateFormat(new SimpleDateFormat(DateTimeStyle.YYYY_MM_DD_HH24_MM_SS.getFormat()))
|
||||
.registerModules(buildCustomModule(), buildJavaTimeModule())
|
||||
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
|
||||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
|
||||
.setSerializationInclusion(Include.NON_NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Java类型转换模块
|
||||
*/
|
||||
private static final Module buildCustomModule() {
|
||||
final SimpleModule customModule = new SimpleModule("CustomModule");
|
||||
// 注意不能转换Long类型数据:请求数据类型变化
|
||||
// customModule.addSerializer(Long.class, ToStringSerializer.instance);
|
||||
return customModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Java时间类型模块
|
||||
*/
|
||||
private static final JavaTimeModule buildJavaTimeModule() {
|
||||
final JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(TimeStyle.HH24_MM_SS.getDateTimeFormatter()));
|
||||
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateStyle.YYYY_MM_DD.getDateTimeFormatter()));
|
||||
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeStyle.YYYY_MM_DD_HH24_MM_SS.getDateTimeFormatter()));
|
||||
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(TimeStyle.HH24_MM_SS.getDateTimeFormatter()));
|
||||
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateStyle.YYYY_MM_DD.getDateTimeFormatter()));
|
||||
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeStyle.YYYY_MM_DD_HH24_MM_SS.getDateTimeFormatter()));
|
||||
return javaTimeModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转List
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param json JSON
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public static final <T> List<T> toList(String json) {
|
||||
if (Objects.isNull(json)) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<List<T>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转List失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转List
|
||||
*
|
||||
* @param <T> Java类型
|
||||
* @param json JSON
|
||||
* @param clazz Java类型
|
||||
*
|
||||
* @return List
|
||||
*/
|
||||
public static final <T> List<T> toList(String json, Class<T> clazz) {
|
||||
if (Objects.isNull(json)) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<List<T>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转List失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Mapper
|
||||
*/
|
||||
public static final ObjectMapper buildMapper() {
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
return mapper
|
||||
.setTimeZone(TimeZone.getDefault())
|
||||
.setDateFormat(new SimpleDateFormat(DateUtils.DateTimeStyle.YYYY_MM_DD_HH24_MM_SS.getFormat()))
|
||||
.registerModules(JSONUtils.buildCustomModule(), JSONUtils.buildJavaTimeModule())
|
||||
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
|
||||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
|
||||
.setSerializationInclusion(Include.NON_NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Java类型转换模块
|
||||
*/
|
||||
private static final Module buildCustomModule() {
|
||||
final SimpleModule customModule = new SimpleModule("CustomModule");
|
||||
// 注意不能转换Long类型数据:请求数据类型变化
|
||||
// customModule.addSerializer(Long.class, ToStringSerializer.instance);
|
||||
return customModule;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Java时间类型模块
|
||||
*/
|
||||
private static final JavaTimeModule buildJavaTimeModule() {
|
||||
final JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateUtils.TimeStyle.HH24_MM_SS.getDateTimeFormatter()));
|
||||
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateUtils.DateStyle.YYYY_MM_DD.getDateTimeFormatter()));
|
||||
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateUtils.DateTimeStyle.YYYY_MM_DD_HH24_MM_SS.getDateTimeFormatter()));
|
||||
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateUtils.TimeStyle.HH24_MM_SS.getDateTimeFormatter()));
|
||||
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateUtils.DateStyle.YYYY_MM_DD.getDateTimeFormatter()));
|
||||
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateUtils.DateTimeStyle.YYYY_MM_DD_HH24_MM_SS.getDateTimeFormatter()));
|
||||
return javaTimeModule;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,10 +14,9 @@ public final class MapUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T> 参数泛型
|
||||
*
|
||||
* @param <T> 参数泛型
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param key 参数名称
|
||||
*
|
||||
* @return 参数值
|
||||
*/
|
||||
@@ -30,10 +29,9 @@ public final class MapUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T> 参数泛型
|
||||
*
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param <T> 参数泛型
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param defaultValue 参数默认值
|
||||
*
|
||||
* @return 参数值
|
||||
@@ -49,7 +47,7 @@ public final class MapUtils {
|
||||
|
||||
/**
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param key 参数名称
|
||||
*
|
||||
* @return 参数值
|
||||
*/
|
||||
@@ -62,17 +60,17 @@ public final class MapUtils {
|
||||
return null;
|
||||
} else if(object instanceof Long value) {
|
||||
return value;
|
||||
} else if(object instanceof Integer value) {
|
||||
return value.longValue();
|
||||
} else if(object instanceof Double value) {
|
||||
return value.longValue();
|
||||
} else if(object instanceof Integer value) {
|
||||
return value.longValue();
|
||||
}
|
||||
return new BigDecimal(object.toString()).longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param key 参数名称
|
||||
*
|
||||
* @return 参数值
|
||||
*/
|
||||
@@ -85,17 +83,17 @@ public final class MapUtils {
|
||||
return null;
|
||||
} else if(object instanceof Long value) {
|
||||
return value.doubleValue();
|
||||
} else if(object instanceof Integer value) {
|
||||
return value.doubleValue();
|
||||
} else if(object instanceof Double value) {
|
||||
return value;
|
||||
} else if(object instanceof Integer value) {
|
||||
return value.doubleValue();
|
||||
}
|
||||
return new BigDecimal(object.toString()).doubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param key 参数名称
|
||||
*
|
||||
* @return 参数值
|
||||
*/
|
||||
@@ -108,17 +106,17 @@ public final class MapUtils {
|
||||
return null;
|
||||
} else if(object instanceof Long value) {
|
||||
return value.intValue();
|
||||
} else if(object instanceof Integer value) {
|
||||
return value;
|
||||
} else if(object instanceof Double value) {
|
||||
return value.intValue();
|
||||
} else if(object instanceof Integer value) {
|
||||
return value;
|
||||
}
|
||||
return new BigDecimal(object.toString()).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param key 参数名称
|
||||
*
|
||||
* @return 参数值
|
||||
*/
|
||||
@@ -136,10 +134,9 @@ public final class MapUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param <T> 参数泛型
|
||||
*
|
||||
* @param <T> 参数泛型
|
||||
* @param body 消息主体
|
||||
* @param key 参数名称
|
||||
* @param key 参数名称
|
||||
*
|
||||
* @return 参数值
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user