|
|
@ -1,8 +1,8 @@ |
|
|
|
/** |
|
|
|
* Copyright (c) 2018 人人开源 All rights reserved. |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* https://www.renren.io
|
|
|
|
* |
|
|
|
* <p> |
|
|
|
* 版权所有,侵权必究! |
|
|
|
*/ |
|
|
|
|
|
|
@ -11,10 +11,14 @@ package com.epmet.commons.tools.security.config; |
|
|
|
import com.epmet.commons.tools.security.resolver.LoginUserHandlerMethodArgumentResolver; |
|
|
|
import com.epmet.commons.tools.security.resolver.UserDetailHandlerMethodArgumentResolver; |
|
|
|
import com.epmet.commons.tools.utils.DateUtils; |
|
|
|
import com.fasterxml.jackson.core.JsonGenerator; |
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature; |
|
|
|
import com.fasterxml.jackson.databind.JsonSerializer; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.fasterxml.jackson.databind.SerializerProvider; |
|
|
|
import com.fasterxml.jackson.databind.module.SimpleModule; |
|
|
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
@ -27,9 +31,10 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage |
|
|
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver; |
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.List; |
|
|
|
import java.util.TimeZone; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
/** |
|
|
|
* MVC配置 |
|
|
@ -43,6 +48,7 @@ public class WebMvcConfig implements WebMvcConfigurer { |
|
|
|
private UserDetailHandlerMethodArgumentResolver userDetailHandlerMethodArgumentResolver; |
|
|
|
@Autowired |
|
|
|
private LoginUserHandlerMethodArgumentResolver loginUserHandlerMethodArgumentResolver; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { |
|
|
|
argumentResolvers.add(loginUserHandlerMethodArgumentResolver); |
|
|
@ -75,6 +81,41 @@ public class WebMvcConfig implements WebMvcConfigurer { |
|
|
|
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); |
|
|
|
mapper.registerModule(simpleModule); |
|
|
|
|
|
|
|
mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { |
|
|
|
@Override |
|
|
|
public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { |
|
|
|
|
|
|
|
String fieldName = jsonGenerator.getOutputContext().getCurrentName(); |
|
|
|
Field field = null; |
|
|
|
try { |
|
|
|
//反射获取字段类型
|
|
|
|
field = jsonGenerator.getCurrentValue().getClass().getDeclaredField(fieldName); |
|
|
|
} catch (NoSuchFieldException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
if (null != field) { |
|
|
|
if (Objects.equals(field.getType(), String.class)) { |
|
|
|
// 字符串型空值""
|
|
|
|
jsonGenerator.writeString(StringUtils.EMPTY); |
|
|
|
return; |
|
|
|
} else if (Objects.equals(field.getType(), List.class)) { |
|
|
|
// 列表型空值返回[]
|
|
|
|
jsonGenerator.writeStartArray(); |
|
|
|
jsonGenerator.writeEndArray(); |
|
|
|
return; |
|
|
|
} else if (Objects.equals(field.getType(), Map.class)) { |
|
|
|
// map型空值返回{}
|
|
|
|
jsonGenerator.writeStartObject(); |
|
|
|
jsonGenerator.writeEndObject(); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 默认返回""
|
|
|
|
jsonGenerator.writeString(StringUtils.EMPTY); |
|
|
|
} |
|
|
|
}); |
|
|
|
converter.setObjectMapper(mapper); |
|
|
|
return converter; |
|
|
|
} |
|
|
|