|
@@ -1,5 +1,11 @@
|
|
|
package org.jeecg.modules.utils;
|
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.MapperFeature;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
import lombok.Data;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
@@ -7,6 +13,9 @@ import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+
|
|
|
/**
|
|
|
* @author 王葆权
|
|
|
* @Title:
|
|
@@ -20,11 +29,18 @@ public class AirportUtil implements ApplicationContextAware {
|
|
|
|
|
|
private static ApplicationContext context;
|
|
|
|
|
|
+ private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
@Override
|
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
context = applicationContext;
|
|
|
}
|
|
|
|
|
|
+ @PostConstruct
|
|
|
+ public void initUtil(){
|
|
|
+ initObjectMapper();
|
|
|
+ }
|
|
|
+
|
|
|
public static Environment getEnvironment() {
|
|
|
return getContext().getEnvironment();
|
|
|
}
|
|
@@ -54,5 +70,53 @@ public class AirportUtil implements ApplicationContextAware {
|
|
|
return getLocalUploadPath() + getEnvironment().getProperty("jeecg.path.temp");
|
|
|
}
|
|
|
|
|
|
+ public static ObjectMapper getObjectMapper() {
|
|
|
+ return objectMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void initObjectMapper() {
|
|
|
+ // 没有匹配的属性名称时不作失败处理
|
|
|
+ //objectMapper.configure(MapperFeature.AUTO_DETECT_FIELDS, true);
|
|
|
+
|
|
|
+ // 反序列化
|
|
|
+ // 禁止遇到空原始类型时抛出异常,用默认值代替。
|
|
|
+ objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
|
|
|
+ //objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
|
|
|
+ // 禁止遇到未知(新)属性时报错,支持兼容扩展
|
|
|
+ //objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
+ //objectMapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
|
|
|
+ objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
|
|
|
+ // 按时间戳格式读取日期
|
|
|
+ // this.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
|
|
|
+ objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
|
|
|
+ objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
|
|
|
+ objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
|
|
+ // 序列化
|
|
|
+ // 禁止序列化空值
|
|
|
+ //objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
|
|
|
+ objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
|
|
+ //objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
|
|
|
+ objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true);
|
|
|
+ // 按时间戳格式生成日期
|
|
|
+ // this.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
|
|
|
+ objectMapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true);
|
|
|
+ objectMapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
|
|
|
+ // 不包含空值属性
|
|
|
+ //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
|
|
|
+ //objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
+ // this.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
|
|
|
+ // 是否缩放排列输出,默认false,有些场合为了便于排版阅读则需要对输出做缩放排列
|
|
|
+ objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
|
|
|
+ // 设置全局的时间转化
|
|
|
+ SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ objectMapper.setDateFormat(smt);
|
|
|
+ // 设置日期反序列化组件
|
|
|
+ //SimpleModule dateDeserializerModule = new SimpleModule();
|
|
|
+ //dateDeserializerModule.addDeserializer(Object.class, new CustomDateDeseralizer());
|
|
|
+ //objectMapper.registerModule(dateDeserializerModule);
|
|
|
+ // 注意:不要开启序列化和反序列化时为对象附加@class属性(坑)
|
|
|
+ // this.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, As.PROPERTY);
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|