|
|
@@ -1,14 +1,24 @@
|
|
|
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.fasterxml.jackson.core.JsonParser;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationContext;
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
|
|
|
+import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
|
|
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
+import com.fasterxml.jackson.databind.ser.std.StringSerializer;
|
|
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
|
|
+import org.python.antlr.ast.Str;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Profile;
|
|
|
+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;
|
|
|
@@ -16,6 +26,8 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
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.*;
|
|
|
|
|
|
@@ -41,6 +53,17 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
|
|
|
SimpleModule booleanSimpleModule = new SimpleModule();
|
|
|
booleanSimpleModule.addSerializer(Boolean.class, new BooleanToIntegerSerializer());
|
|
|
booleanSimpleModule.addSerializer(Boolean.TYPE, new BooleanToIntegerSerializer());
|
|
|
+
|
|
|
+ SimpleModule stringModule = new SimpleModule();
|
|
|
+ stringModule.addSerializer(String.class,new StringSerializer());
|
|
|
+ stringModule.addDeserializer(String.class, new StdScalarDeserializer<String>(String.class) {
|
|
|
+ @Override
|
|
|
+ public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
|
|
+ String text = p.getText();
|
|
|
+ return StrUtil.isBlankIfStr(text)?null:text;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ objectMapper.registerModule(stringModule);
|
|
|
objectMapper.registerModule(booleanSimpleModule);
|
|
|
// 设置格式化内容
|
|
|
converter.setObjectMapper(objectMapper);
|
|
|
@@ -74,6 +97,18 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @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;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|