|
@@ -0,0 +1,357 @@
|
|
|
+package com.nh.farm.cloud.config;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author 张智凯
|
|
|
+ * @version 1.0
|
|
|
+ * @data 2022/10/11 15:08
|
|
|
+ */
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JacksonException;
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
+import com.fasterxml.jackson.core.JsonParser;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.*;
|
|
|
+import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
+import com.fasterxml.jackson.databind.ser.std.DateSerializer;
|
|
|
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
|
|
|
+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 org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.Reader;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.sql.Clob;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.text.DateFormat;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.*;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.format.DateTimeFormatterBuilder;
|
|
|
+import java.time.temporal.ChronoField;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
+
|
|
|
+import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
|
|
|
+ * 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
|
|
|
+ * 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class JacksonObjectMapper extends ObjectMapper {
|
|
|
+
|
|
|
+ public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
|
|
+ public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
|
|
|
+
|
|
|
+ public static final List<String> formarts = new ArrayList<String>(14);
|
|
|
+ static{
|
|
|
+ formarts.add("yyyy-MM");
|
|
|
+ formarts.add("yyyy-MM-dd");
|
|
|
+ formarts.add("yyyy-MM-dd HH:mm");
|
|
|
+ formarts.add("yyyy-MM-dd HH:mm:ss");
|
|
|
+ formarts.add("yyyy-MM-dd'T'HH:mm:ss");
|
|
|
+ //国际标准时间
|
|
|
+
|
|
|
+ formarts.add("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
+ formarts.add("yyyy-MM-dd'T'HH:mm");
|
|
|
+
|
|
|
+
|
|
|
+ formarts.add("yyyy/MM");
|
|
|
+ formarts.add("yyyy/MM/dd");
|
|
|
+ formarts.add("yyyy/MM/dd HH:mm");
|
|
|
+ formarts.add("yyyy/MM/dd HH:mm:ss");
|
|
|
+ formarts.add("yyyy/MM/dd'T'HH:mm:ss");
|
|
|
+ //国际标准时间
|
|
|
+ formarts.add("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
|
|
+ formarts.add("yyyy/MM/dd'T'HH:mm");
|
|
|
+ }
|
|
|
+
|
|
|
+ public JacksonObjectMapper() {
|
|
|
+ super();
|
|
|
+ //收到未知属性时不报异常
|
|
|
+ this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
+
|
|
|
+ //反序列化时,属性不存在的兼容处理
|
|
|
+ this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
|
|
+
|
|
|
+ SimpleModule simpleModule = new SimpleModule()
|
|
|
+ .addDeserializer(LocalDateTime.class, new MyLocalDateTimeDeserializer())
|
|
|
+ .addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
|
|
|
+ .addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
|
|
|
+
|
|
|
+ .addSerializer(BigInteger.class, ToStringSerializer.instance)
|
|
|
+ .addSerializer(Long.class, ToStringSerializer.instance)
|
|
|
+ .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
|
|
|
+ .addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
|
|
|
+ .addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
|
|
|
+
|
|
|
+ //日期序列化、反序列化
|
|
|
+ simpleModule.addSerializer(Date.class,new DateSerializer(false,new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT)));
|
|
|
+ simpleModule.addDeserializer(Date.class, new DateDeserializer());
|
|
|
+ simpleModule.addSerializer(Clob.class,new ClobSerializer());
|
|
|
+ //注册功能模块 例如,可以添加自定义序列化器和反序列化器
|
|
|
+ this.registerModule(simpleModule);
|
|
|
+ }
|
|
|
+
|
|
|
+ public class DateDeserializer extends JsonDeserializer<Date> {
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
|
|
+ String source = p.getText().trim();
|
|
|
+ if ("".equals(source)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(source.matches("^\\d{4}-\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(0));
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(1));
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(2));
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(3));
|
|
|
+ } else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(4));
|
|
|
+ } else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}.0{3}Z$")){
|
|
|
+ source = source.replace("Z", " UTC");
|
|
|
+ return parseDate(source, formarts.get(5));
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(6));
|
|
|
+ }
|
|
|
+ if(source.matches("^\\d{4}/\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(7));
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(8));
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(9));
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(10));
|
|
|
+ } else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(11));
|
|
|
+ } else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}.0{3}Z$")){
|
|
|
+ source = source.replace("Z", " UTC");
|
|
|
+ return parseDate(source, formarts.get(12));
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}T{1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(13));
|
|
|
+ }
|
|
|
+ else if(source.matches("^\\d{13}$")){//毫秒
|
|
|
+ return new Date(Long.parseLong(source));
|
|
|
+ }
|
|
|
+ else if(source.matches("^\\d{10}$")){//秒
|
|
|
+ return new Date(Long.parseLong(source) * 1000);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ throw new IllegalArgumentException("Invalid Date value '" + source + "'");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述:格式化日期
|
|
|
+ * @param dateStr String 字符型日期
|
|
|
+ * @param format String 格式
|
|
|
+ * @return Date 日期
|
|
|
+ */
|
|
|
+ public Date parseDate(String dateStr, String format) {
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ DateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
+ date = dateFormat.parse(dateStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return date;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public class MyLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LocalDateTime deserialize(JsonParser p, DeserializationContext ctcx) throws IOException, JacksonException {
|
|
|
+ String source = p.getText().trim();
|
|
|
+ if ("".equals(source)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(source.matches("^\\d{4}-\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(0),"yyyy-MM");
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(1),"yyyy-MM-dd");
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(2));
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(3));
|
|
|
+ } else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(4));
|
|
|
+ } else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}.0{3}Z$")){
|
|
|
+ // source = source.replace("Z", " UTC");
|
|
|
+ return parseDate(source, formarts.get(5),"UTC");
|
|
|
+ }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}T{1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(6));
|
|
|
+ }
|
|
|
+ if(source.matches("^\\d{4}/\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(7),"yyyy-MM");
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(8),"yyyy-MM-dd");
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(9));
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(10));
|
|
|
+ } else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(11));
|
|
|
+ } else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}T{1}\\d{1,2}:\\d{1,2}:\\d{1,2}.0{3}Z$")){
|
|
|
+ // source = source.replace("Z", " UTC");
|
|
|
+ return parseDate(source, formarts.get(12),"UTC");
|
|
|
+ }else if(source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}T{1}\\d{1,2}:\\d{1,2}$")){
|
|
|
+ return parseDate(source, formarts.get(13));
|
|
|
+ }
|
|
|
+ else if(source.matches("^\\d{13}$")){//毫秒
|
|
|
+ return new Date(Long.parseLong(source)).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
|
|
|
+ }
|
|
|
+ else if(source.matches("^\\d{10}$")){//秒
|
|
|
+ return new Date(Long.parseLong(source) * 1000).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ throw new IllegalArgumentException("Invalid LocalDateTime value '" + source + "'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述:格式化日期
|
|
|
+ * @param dateStr String 字符型日期
|
|
|
+ * @param format String 格式
|
|
|
+ * @return LocalDateTime 日期*/
|
|
|
+
|
|
|
+ public LocalDateTime parseDate(String dateStr, String format,String parser) {
|
|
|
+ LocalDateTime dateTime = null;
|
|
|
+ try {
|
|
|
+ if(parser.equals("yyyy-MM")){
|
|
|
+ DateTimeFormatter fmt = new DateTimeFormatterBuilder()
|
|
|
+ .appendPattern(format)
|
|
|
+ .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
|
|
|
+ .toFormatter();
|
|
|
+ LocalDate localDate = LocalDate.parse(dateStr, fmt);
|
|
|
+ dateTime = localDate.atStartOfDay();
|
|
|
+ }else if(parser.equals("yyyy-MM-dd")){
|
|
|
+ LocalDate localDate = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(format));
|
|
|
+ dateTime = localDate.atStartOfDay();
|
|
|
+ }else if(parser.equals("UTC")){
|
|
|
+ DateTimeFormatter df = DateTimeFormatter.ofPattern(format, Locale.ENGLISH);
|
|
|
+ LocalDateTime ldt = LocalDateTime.parse(dateStr, df);
|
|
|
+ ZoneId currentZone = ZoneId.of("UTC");
|
|
|
+ dateTime = ldt.atZone(currentZone).withZoneSameInstant(ZoneOffset.ofHours(8)).toLocalDateTime();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述:格式化日期
|
|
|
+ * @param dateStr String 字符型日期
|
|
|
+ * @param format String 格式
|
|
|
+ * @return LocalDateTime 日期
|
|
|
+ */
|
|
|
+ public LocalDateTime parseDate(String dateStr, String format) {
|
|
|
+ LocalDateTime dateTime = null;
|
|
|
+ try {
|
|
|
+ dateTime = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(format));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return dateTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public class ClobSerializer extends JsonSerializer<Clob> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serialize(Clob clob, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
|
|
|
+ throws IOException {
|
|
|
+ try {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ Reader reader = clob.getCharacterStream();
|
|
|
+ BufferedReader br = new BufferedReader(reader);
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ sb.append(line);
|
|
|
+ }
|
|
|
+ br.close();
|
|
|
+ jsonGenerator.writeString(sb.toString());
|
|
|
+ } catch (SQLException e) {
|
|
|
+ // 处理异常情况
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+/*
|
|
|
+ int input = 257;
|
|
|
+ int i = input;
|
|
|
+ int a = 0;
|
|
|
+ Boolean bo = true;
|
|
|
+
|
|
|
+ if(input%64 == 0){
|
|
|
+ System.out.println(input);
|
|
|
+ }else{
|
|
|
+ while (true){
|
|
|
+ a ++;
|
|
|
+ i = i - 1;
|
|
|
+ if(i <= 0){
|
|
|
+ bo = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if(i%64 == 0){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int b = 0;
|
|
|
+ int i1 = input;
|
|
|
+ while (true){
|
|
|
+ b ++;
|
|
|
+ i1 = i1 + 1;
|
|
|
+ if(i1%64 == 0){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(a < b && bo){
|
|
|
+ System.out.println(i);
|
|
|
+ }else{
|
|
|
+ System.out.println(i1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ int num = 257;
|
|
|
+ if(num<64){
|
|
|
+ System.out.println(64);
|
|
|
+ }else {
|
|
|
+ int round = Math.round(num / 64);
|
|
|
+ System.out.println(round * 64);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|