JsonMapper 使用 DateUtils 解析日期,以支持更多的格式;优化 JsonFormat 注解非日期类型的自定义
This commit is contained in:
@@ -19,14 +19,15 @@ import com.jeesite.common.codec.EncodeUtils;
|
|||||||
import com.jeesite.common.collect.ListUtils;
|
import com.jeesite.common.collect.ListUtils;
|
||||||
import com.jeesite.common.io.PropertiesUtils;
|
import com.jeesite.common.io.PropertiesUtils;
|
||||||
import com.jeesite.common.lang.DateUtils;
|
import com.jeesite.common.lang.DateUtils;
|
||||||
|
import org.apache.commons.lang3.LocaleUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.Serial;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -39,6 +40,7 @@ import java.util.TimeZone;
|
|||||||
*/
|
*/
|
||||||
public class JsonMapper extends ObjectMapper {
|
public class JsonMapper extends ObjectMapper {
|
||||||
|
|
||||||
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(JsonMapper.class);
|
private static final Logger logger = LoggerFactory.getLogger(JsonMapper.class);
|
||||||
@@ -51,49 +53,41 @@ public class JsonMapper extends ObjectMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public JsonMapper() {
|
public JsonMapper() {
|
||||||
// Spring ObjectMapper 初始化配置,支持 @JsonView
|
// 设置默认时区、默认日期格式
|
||||||
new Jackson2ObjectMapperBuilder().configure(this);
|
this.setLocaleTimeZoneDateFormat();
|
||||||
// 为Null时不序列化
|
// 为Null时不序列化
|
||||||
this.setSerializationInclusion(Include.NON_NULL);
|
this.setSerializationInclusion(Include.NON_NULL);
|
||||||
// 允许单引号
|
// 允许单引号
|
||||||
this.configure(Feature.ALLOW_SINGLE_QUOTES, true);
|
this.configure(Feature.ALLOW_SINGLE_QUOTES, true);
|
||||||
// 允许不带引号的字段名称
|
// 允许不带引号的字段名称
|
||||||
this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
|
||||||
// 设置默认时区
|
// 遇到空值处理为空串
|
||||||
this.setDefaultTimeZone();
|
|
||||||
// 设置默认日期格式
|
|
||||||
this.setDefaultDateFormat();
|
|
||||||
// 遇到空值处理为空串
|
|
||||||
this.enabledNullValueToEmpty();
|
this.enabledNullValueToEmpty();
|
||||||
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
|
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
|
||||||
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||||
|
// Spring ObjectMapper 初始化配置,支持 @JsonView
|
||||||
|
new Jackson2ObjectMapperBuilder().configure(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开启日期类型默认格式化
|
* 设置默认时区、默认日期格式
|
||||||
* @author ThinkGem
|
* @author ThinkGem
|
||||||
*/
|
*/
|
||||||
public JsonMapper setDefaultTimeZone(){
|
public JsonMapper setLocaleTimeZoneDateFormat(){
|
||||||
|
this.setLocale(LocaleUtils.toLocale(PropertiesUtils.getInstance()
|
||||||
|
.getProperty("lang.defaultLocale", "zh_CN")));
|
||||||
this.setTimeZone(TimeZone.getTimeZone(PropertiesUtils.getInstance()
|
this.setTimeZone(TimeZone.getTimeZone(PropertiesUtils.getInstance()
|
||||||
.getProperty("lang.defaultTimeZone", "GMT+08:00")));
|
.getProperty("lang.defaultTimeZone", "GMT+08:00")));
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 开启日期类型默认格式化
|
|
||||||
* @author ThinkGem
|
|
||||||
*/
|
|
||||||
public JsonMapper setDefaultDateFormat(){
|
|
||||||
this.setDateFormat(new SimpleDateFormat(PropertiesUtils.getInstance()
|
|
||||||
.getProperty("web.json.defaultDateFormat", "yyyy-MM-dd HH:mm:ss")));
|
|
||||||
this.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
|
this.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
|
||||||
|
@Serial
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
@Override
|
@Override
|
||||||
public Object findSerializer(Annotated a) {
|
public Object findSerializer(Annotated a) {
|
||||||
if (a instanceof AnnotatedMethod) {
|
if (a instanceof AnnotatedMethod) {
|
||||||
AnnotatedElement m = a.getAnnotated();
|
AnnotatedElement m = a.getAnnotated();
|
||||||
JsonFormat jf = m.getAnnotation(JsonFormat.class);
|
JsonFormat jf = m.getAnnotation(JsonFormat.class);
|
||||||
if (jf != null) {
|
if (jf != null && StringUtils.containsAnyIgnoreCase(jf.pattern(),
|
||||||
|
"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS")) {
|
||||||
return new JsonSerializer<Date>(){
|
return new JsonSerializer<Date>(){
|
||||||
@Override
|
@Override
|
||||||
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
||||||
@@ -101,11 +95,48 @@ public class JsonMapper extends ObjectMapper {
|
|||||||
jgen.writeString(DateUtils.formatDate(value, jf.pattern()));
|
jgen.writeString(DateUtils.formatDate(value, jf.pattern()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
AnnotatedMethod am = (AnnotatedMethod) a;
|
||||||
|
if (am.getRawReturnType() == Date.class) {
|
||||||
|
return new JsonSerializer<Date>(){
|
||||||
|
@Override
|
||||||
|
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
||||||
|
if (value != null){
|
||||||
|
jgen.writeString(DateUtils.formatDateTime(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return super.findSerializer(a);
|
return super.findSerializer(a);
|
||||||
}
|
}
|
||||||
|
@Override
|
||||||
|
public Object findDeserializer(Annotated a) {
|
||||||
|
if (a instanceof AnnotatedMethod) {
|
||||||
|
AnnotatedElement m = a.getAnnotated();
|
||||||
|
JsonFormat jf = m.getAnnotation(JsonFormat.class);
|
||||||
|
if (jf != null && StringUtils.containsAnyIgnoreCase(jf.pattern(),
|
||||||
|
"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS")) {
|
||||||
|
return new JsonDeserializer<Date>(){
|
||||||
|
@Override
|
||||||
|
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
return DateUtils.parseDate(p.getText(), jf.pattern());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
AnnotatedMethod am = (AnnotatedMethod) a;
|
||||||
|
if (am.getParameterCount() > 0 && am.getParameterType(0).getRawClass() == Date.class) {
|
||||||
|
return new JsonDeserializer<Date>(){
|
||||||
|
@Override
|
||||||
|
public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
return DateUtils.parseDate(p.getText());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return super.findDeserializer(a);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -120,7 +151,7 @@ public class JsonMapper extends ObjectMapper {
|
|||||||
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
|
||||||
jgen.writeString(StringUtils.EMPTY);
|
jgen.writeString(StringUtils.EMPTY);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user