A17404李放 пре 3 година
родитељ
комит
8afbe6a3dd

+ 3 - 3
coffee-common/src/main/java/com/coffee/common/cache/value/SimpleValue.java

@@ -40,7 +40,7 @@ public class SimpleValue implements Value {
         }
         if (targetClass == String.class) {
             if (source instanceof Date) {
-                return (T) DateUtil.format(((Date) source), "yyyy-MM-dd HH:mm:ss");
+                return (T) DateUtil.parse(String.valueOf(source));
             }
             return (T) String.valueOf(source);
         }
@@ -49,7 +49,7 @@ public class SimpleValue implements Value {
         }
         if (targetClass == Date.class) {
             if (source instanceof String) {
-               return (T) DateUtil.parse(String.valueOf(source),"yyyy-MM-dd HH:mm:ss");
+               return (T) DateUtil.parse(String.valueOf(source));
             }
             if (source instanceof Number) {
                 return (T) new Date(((Number) source).longValue());
@@ -66,7 +66,7 @@ public class SimpleValue implements Value {
                         Field value = enumConstant.getClass().getDeclaredField("value");
                         value.setAccessible(true);
                         try {
-                            if(ObjectUtil.equal(value.get(enumConstant),source)){
+                            if(ObjectUtil.equal(String.valueOf(value.get(enumConstant)),String.valueOf(source))){
                                 return enumConstant;
                             }
                         } catch (IllegalAccessException e) {

+ 7 - 20
coffee-framework/src/main/java/com/coffee/framework/config/WebAppMvcConfig.java

@@ -3,6 +3,7 @@ package com.coffee.framework.config;
 import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.util.StrUtil;
 import com.coffee.common.config.BooleanToIntegerSerializer;
+import com.coffee.framework.config.convert.EnumDeserializer;
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -11,9 +12,8 @@ import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
 import com.fasterxml.jackson.databind.module.SimpleModule;
 import com.fasterxml.jackson.databind.ser.std.StringSerializer;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.format.Formatter;
-import org.springframework.format.FormatterRegistry;
 import org.springframework.http.converter.HttpMessageConverter;
 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
 import org.springframework.web.servlet.HandlerInterceptor;
@@ -22,7 +22,6 @@ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 import java.io.IOException;
-import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.*;
 
@@ -33,6 +32,7 @@ import java.util.*;
  * @Created by jianxiapc
  */
 @Configuration
+@EnableAutoConfiguration
 //@Profile("dev")
 public class WebAppMvcConfig implements WebMvcConfigurer {
     @Autowired
@@ -42,7 +42,6 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
     @Override
     public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
         MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
-//        ObjectMapper objectMapper = converter.getObjectMapper();
         // 时间格式化
         SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
         serializerProvider.setNullValueSerializer(new JsonSerializer<Object>() {
@@ -67,6 +66,10 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
                 return StrUtil.isBlankIfStr(text)?null:text;
             }
         });
+        SimpleModule enumModule = new SimpleModule();
+        enumModule.addDeserializer(Enum.class, new EnumDeserializer());
+        objectMapper.registerModule(enumModule);
+
         objectMapper.registerModule(stringModule);
         objectMapper.registerModule(booleanSimpleModule);
         // 设置格式化内容
@@ -99,20 +102,4 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
             interceptors.forEach(registry::addInterceptor);
         }
     }
-
-
-    @Override
-    public void addFormatters(FormatterRegistry registry) {
-        registry.addFormatterForFieldType(String.class, new Formatter<String>() {
-            @Override
-            public String parse(String text, Locale locale) throws ParseException {
-                return StrUtil.isBlankIfStr(text)?null:text;
-            }
-
-            @Override
-            public String print(String object, Locale locale) {
-                return object;
-            }
-        });
-    }
 }

+ 43 - 0
coffee-framework/src/main/java/com/coffee/framework/config/convert/EnumConvertFactory.java

@@ -0,0 +1,43 @@
+package com.coffee.framework.config.convert;
+
+import com.baomidou.mybatisplus.annotation.IEnum;
+import org.springframework.core.convert.converter.Converter;
+import org.springframework.core.convert.converter.ConverterFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+@Component
+public class EnumConvertFactory implements ConverterFactory<String, IEnum<?>> {
+
+    @Override
+    public <T extends IEnum<?>> Converter<String, T> getConverter(Class<T> targetType) {
+        return new StringToEnum<>(targetType);
+    }
+
+
+    public static class StringToEnum<T extends IEnum<?>> implements Converter<String, T> {
+
+        private final Class<T> targetType;
+
+        public StringToEnum(Class<T> targetType) {
+            this.targetType = targetType;
+        }
+
+        @Override
+        public T convert(String source) {
+            if (!StringUtils.hasText(source)) {
+                return null;
+            }
+            return (T) EnumConvertFactory.getEnum(this.targetType, source);
+        }
+    }
+
+    public static <T extends IEnum<?>> T getEnum(Class<T> targetType, String source) {
+        for (T constant : targetType.getEnumConstants()) {
+            if (source.equals(String.valueOf(constant.getValue()))) {
+                return constant;
+            }
+        }
+        return null;
+    }
+}

+ 59 - 0
coffee-framework/src/main/java/com/coffee/framework/config/convert/EnumDeserializer.java

@@ -0,0 +1,59 @@
+package com.coffee.framework.config.convert;
+
+import com.baomidou.mybatisplus.annotation.IEnum;
+import com.coffee.common.cache.value.Value;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.BeanProperty;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import org.springframework.util.StringUtils;
+
+import java.io.IOException;
+
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class EnumDeserializer extends JsonDeserializer<Enum<?>> implements ContextualDeserializer {
+
+    private Class<?> target;
+
+    @SuppressWarnings("all")
+    @Override
+    public Enum<?> deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException {
+        if (!StringUtils.hasText(jsonParser.getText())) {
+            return null;
+        }
+        if (IEnum.class.isAssignableFrom(target)) {
+            return (Enum<?>) EnumConvertFactory.getEnum((Class) target, jsonParser.getText());
+        }
+       return (Enum<?>) Value.simple(jsonParser.getText()).as(target);
+    }
+
+    /**
+     * @param ctx      ctx
+     * @param property property
+     * @return 1
+     * @throws JsonMappingException
+     */
+    @Override
+    public JsonDeserializer<?> createContextual(DeserializationContext ctx, BeanProperty property) throws JsonMappingException {
+        Class<?> rawCls = ctx.getContextualType().getRawClass();
+        EnumDeserializer enumDeserializer = new EnumDeserializer();
+        enumDeserializer.setTarget(rawCls);
+        return enumDeserializer;
+    }
+
+
+    public static Enum<?> defaultEnumTransform(Class<?> type, String indexString) {
+        Enum<?>[] enumConstants = (Enum<?>[]) type.getEnumConstants();
+        try {
+            int index = Integer.parseInt(indexString);
+            return enumConstants[index];
+        } catch (NumberFormatException e) {
+            return null;
+        }
+    }
+}