mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
♻️ Refactoring code. 增加序列化规则 Duration 类型
This commit is contained in:
1 parent
a18aea8f3b
commit
55bb573bd4
2 files changed
+86
-86
No files matched your search
+39
-42
@@ -3,19 +3,10 @@ package com.pig4cloud.pigx.common.core.jackson;
|
||||
import cn.hutool.core.date.DatePattern;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.InstantSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.*;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.*;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
@@ -26,39 +17,45 @@ import java.time.format.DateTimeFormatter;
|
||||
*/
|
||||
public class PigxJavaTimeModule extends SimpleModule {
|
||||
|
||||
/**
|
||||
* 指定序列化规则
|
||||
*/
|
||||
public PigxJavaTimeModule() {
|
||||
super(PackageVersion.VERSION);
|
||||
/**
|
||||
* 指定序列化规则
|
||||
*/
|
||||
public PigxJavaTimeModule() {
|
||||
super(PackageVersion.VERSION);
|
||||
|
||||
// ======================= 时间序列化规则 ===============================
|
||||
// yyyy-MM-dd HH:mm:ss
|
||||
this.addSerializer(LocalDateTime.class,
|
||||
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
|
||||
// yyyy-MM-dd
|
||||
this.addSerializer(LocalDate.class,
|
||||
new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
|
||||
// HH:mm:ss
|
||||
this.addSerializer(LocalTime.class,
|
||||
new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
|
||||
// ======================= 时间序列化规则 ===============================
|
||||
// yyyy-MM-dd HH:mm:ss
|
||||
this.addSerializer(LocalDateTime.class,
|
||||
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
|
||||
// yyyy-MM-dd
|
||||
this.addSerializer(LocalDate.class,
|
||||
new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
|
||||
// HH:mm:ss
|
||||
this.addSerializer(LocalTime.class,
|
||||
new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
|
||||
|
||||
// Instant 类型序列化
|
||||
this.addSerializer(Instant.class, InstantSerializer.INSTANCE);
|
||||
// Instant 类型序列化
|
||||
this.addSerializer(Instant.class, InstantSerializer.INSTANCE);
|
||||
|
||||
// ======================= 时间反序列化规则 ==============================
|
||||
// yyyy-MM-dd HH:mm:ss
|
||||
this.addDeserializer(LocalDateTime.class,
|
||||
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
|
||||
// yyyy-MM-dd
|
||||
this.addDeserializer(LocalDate.class,
|
||||
new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
|
||||
// HH:mm:ss
|
||||
this.addDeserializer(LocalTime.class,
|
||||
new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
|
||||
// Instant 反序列化
|
||||
this.addDeserializer(Instant.class, InstantDeserializer.INSTANT);
|
||||
// Duration 类型序列化
|
||||
this.addSerializer(Duration.class, DurationSerializer.INSTANCE);
|
||||
|
||||
}
|
||||
// ======================= 时间反序列化规则 ==============================
|
||||
// yyyy-MM-dd HH:mm:ss
|
||||
this.addDeserializer(LocalDateTime.class,
|
||||
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
|
||||
// yyyy-MM-dd
|
||||
this.addDeserializer(LocalDate.class,
|
||||
new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
|
||||
// HH:mm:ss
|
||||
this.addDeserializer(LocalTime.class,
|
||||
new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
|
||||
// Instant 反序列化
|
||||
this.addDeserializer(Instant.class, InstantDeserializer.INSTANT);
|
||||
|
||||
// Duration 反序列化
|
||||
this.addDeserializer(Duration.class, DurationDeserializer.INSTANCE);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+47
-44
@@ -19,6 +19,7 @@
|
||||
|
||||
package com.pig4cloud.pigx.common.core.util;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
@@ -40,61 +41,63 @@ import java.io.Serializable;
|
||||
@Schema(description = "响应信息主体")
|
||||
public class R<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Schema(description = "返回标记:成功标记=0,失败标记=1")
|
||||
private int code;
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonPropertyDescription(value = "调用成功返回0,调用失败返回1")
|
||||
@Schema(description = "返回标记:成功标记=0,失败标记=1")
|
||||
private int code;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Schema(description = "返回信息")
|
||||
private String msg;
|
||||
@Getter
|
||||
@Setter
|
||||
@JsonPropertyDescription(value = "返回具体的信息")
|
||||
@Schema(description = "返回信息")
|
||||
private String msg;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Schema(description = "数据")
|
||||
private T data;
|
||||
@Getter
|
||||
@Setter
|
||||
@Schema(description = "数据")
|
||||
private T data;
|
||||
|
||||
public static <T> R<T> ok() {
|
||||
return restResult(null, CommonConstants.SUCCESS, null);
|
||||
}
|
||||
public static <T> R<T> ok() {
|
||||
return restResult(null, CommonConstants.SUCCESS, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data) {
|
||||
return restResult(data, CommonConstants.SUCCESS, null);
|
||||
}
|
||||
public static <T> R<T> ok(T data) {
|
||||
return restResult(data, CommonConstants.SUCCESS, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(T data, String msg) {
|
||||
return restResult(data, CommonConstants.SUCCESS, msg);
|
||||
}
|
||||
public static <T> R<T> ok(T data, String msg) {
|
||||
return restResult(data, CommonConstants.SUCCESS, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed() {
|
||||
return restResult(null, CommonConstants.FAIL, null);
|
||||
}
|
||||
public static <T> R<T> failed() {
|
||||
return restResult(null, CommonConstants.FAIL, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(String msg) {
|
||||
return restResult(null, CommonConstants.FAIL, msg);
|
||||
}
|
||||
public static <T> R<T> failed(String msg) {
|
||||
return restResult(null, CommonConstants.FAIL, msg);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(T data) {
|
||||
return restResult(data, CommonConstants.FAIL, null);
|
||||
}
|
||||
public static <T> R<T> failed(T data) {
|
||||
return restResult(data, CommonConstants.FAIL, null);
|
||||
}
|
||||
|
||||
public static <T> R<T> failed(T data, String msg) {
|
||||
return restResult(data, CommonConstants.FAIL, msg);
|
||||
}
|
||||
public static <T> R<T> failed(T data, String msg) {
|
||||
return restResult(data, CommonConstants.FAIL, msg);
|
||||
}
|
||||
|
||||
static <T> R<T> restResult(T data, int code, String msg) {
|
||||
R<T> apiResult = new R<>();
|
||||
apiResult.setCode(code);
|
||||
apiResult.setData(data);
|
||||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
static <T> R<T> restResult(T data, int code, String msg) {
|
||||
R<T> apiResult = new R<>();
|
||||
apiResult.setCode(code);
|
||||
apiResult.setData(data);
|
||||
apiResult.setMsg(msg);
|
||||
return apiResult;
|
||||
}
|
||||
|
||||
public boolean isOk() {
|
||||
return this.code == CommonConstants.SUCCESS;
|
||||
}
|
||||
public boolean isOk() {
|
||||
return this.code == CommonConstants.SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user