|
@@ -1,32 +1,18 @@
|
|
|
package com.nb.common.config;
|
|
package com.nb.common.config;
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
|
|
-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.*;
|
|
|
-import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
|
|
|
|
|
-import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
|
|
-import com.fasterxml.jackson.databind.ser.std.StringSerializer;
|
|
|
|
|
-import com.nb.common.config.serializer.BooleanToIntegerSerializer;
|
|
|
|
|
-import com.nb.common.config.serializer.EnumDeserializer;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
|
|
|
|
-import org.springframework.context.annotation.Lazy;
|
|
|
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
-
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -35,16 +21,13 @@ import java.util.*;
|
|
|
* @Date 2021/3/2 15:27
|
|
* @Date 2021/3/2 15:27
|
|
|
* @Created by jianxiapc
|
|
* @Created by jianxiapc
|
|
|
*/
|
|
*/
|
|
|
-@Configuration
|
|
|
|
|
|
|
+@AllArgsConstructor
|
|
|
@EnableAutoConfiguration
|
|
@EnableAutoConfiguration
|
|
|
-//@EnableAspectJAutoProxy(exposeProxy = true)
|
|
|
|
|
|
|
+@Configuration
|
|
|
public class WebAppMvcConfig implements WebMvcConfigurer {
|
|
public class WebAppMvcConfig implements WebMvcConfigurer {
|
|
|
- @Autowired
|
|
|
|
|
- @Lazy
|
|
|
|
|
- private ObjectMapper objectMapper;
|
|
|
|
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
|
|
|
|
|
- @Autowired(required = false)
|
|
|
|
|
- private List<HandlerInterceptor> interceptors;
|
|
|
|
|
|
|
+ private final List<HandlerInterceptor> interceptors;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
@@ -55,59 +38,6 @@ public class WebAppMvcConfig implements WebMvcConfigurer {
|
|
|
@Override
|
|
@Override
|
|
|
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
|
|
- // 时间格式化
|
|
|
|
|
- SerializerProvider serializerProvider = objectMapper.getSerializerProvider();
|
|
|
|
|
- serializerProvider.setNullValueSerializer(new JsonSerializer<Object>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
|
|
|
|
- gen.writeNull();
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
- objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
-
|
|
|
|
|
- SimpleModule booleanSimpleModule = new SimpleModule();
|
|
|
|
|
- booleanSimpleModule.addSerializer(Boolean.class, new BooleanToIntegerSerializer());
|
|
|
|
|
- booleanSimpleModule.addDeserializer(Boolean.class, new StdScalarDeserializer<Boolean>(Object.class) {
|
|
|
|
|
- @Override
|
|
|
|
|
- public Boolean deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
|
|
|
|
- String text = p.getText();
|
|
|
|
|
- if (StrUtil.isBlank(text)) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- String lowerCase = text.toLowerCase();
|
|
|
|
|
- if("true".equals(lowerCase)||"1".equals(lowerCase)){
|
|
|
|
|
- return Boolean.TRUE;
|
|
|
|
|
- }else if("false".equals(lowerCase)||"0".equals(lowerCase)){
|
|
|
|
|
- return Boolean.FALSE;
|
|
|
|
|
- }
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- 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;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- SimpleModule enumModule = new SimpleModule();
|
|
|
|
|
- enumModule.addDeserializer(Enum.class, new EnumDeserializer());
|
|
|
|
|
-
|
|
|
|
|
-// objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
|
|
|
|
|
-// @Override
|
|
|
|
|
-// public void serialize(Object param, JsonGenerator jsonGenerator,
|
|
|
|
|
-// SerializerProvider paramSerializerProvider) throws IOException {
|
|
|
|
|
-// jsonGenerator.writeString("");
|
|
|
|
|
-// }
|
|
|
|
|
-// });
|
|
|
|
|
-
|
|
|
|
|
- objectMapper.registerModule(enumModule);
|
|
|
|
|
- objectMapper.registerModule(stringModule);
|
|
|
|
|
- objectMapper.registerModule(booleanSimpleModule);
|
|
|
|
|
// 设置格式化内容
|
|
// 设置格式化内容
|
|
|
converter.setObjectMapper(objectMapper);
|
|
converter.setObjectMapper(objectMapper);
|
|
|
converters.add(0, converter);
|
|
converters.add(0, converter);
|