Created
January 25, 2019 01:16
-
-
Save gcdd1993/c015668b72e3ebcb2112a946efa7ae5d to your computer and use it in GitHub Desktop.
Springboot整合Jackson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.maxtropy.imep.config.jackson; | |
import ch.mfrey.jackson.antpathfilter.AntPathFilterMixin; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import lombok.Data; | |
import lombok.extern.slf4j.Slf4j; | |
import org.reflections.Reflections; | |
import org.reflections.scanners.SubTypesScanner; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; | |
import java.util.*; | |
/** | |
* Created by hanxiao on 2017/4/18. | |
*/ | |
@Data | |
@Configuration | |
@ConfigurationProperties(prefix = "jackson.mix") | |
@Slf4j | |
public class JacksonAutoConfiguration { | |
private Set<String> includeClass; //需要包含到antPathFilter的Class列表 | |
private Set<String> excludeClass; //需要排除出antPathFilter的Class列表 | |
private Set<String> includePackage; //需要包含到antPathFilter的package列表 | |
private Set<String> excludePackage; //需要排除出antPathFilter的package列表 | |
@Bean | |
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { | |
return new Jackson2ObjectMapperBuilder() | |
.modules(new CustomModule()) | |
.timeZone(TimeZone.getDefault()) | |
.failOnEmptyBeans(false) | |
.failOnUnknownProperties(false) | |
.mixIns(mixMap()) | |
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | |
.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); | |
} | |
@Bean | |
public ObjectMapper objectMapper() { | |
return jackson2ObjectMapperBuilder().build(); | |
} | |
private HashMap<Class<?>, Class<?>> mixMap() { | |
HashMap<Class<?>, Class<?>> mixMap = new HashMap<>(); | |
Set<Class<?>> includeClassSet = buildClassSet(includePackage, null); //包含 | |
Set<Class<?>> excludeClassSet = buildClassSet(excludePackage, null); //排除 | |
Set<Class<?>> forceIncludeClassSet = buildClassSet(null, includeClass); //强制包含 | |
Set<Class<?>> forceExcludeClassSet = buildClassSet(null, excludeClass); //强制排除 | |
//将强制包含从排除列表删除 | |
excludeClassSet.removeAll(forceIncludeClassSet); | |
//将强制排除从包含列表删除 | |
includeClassSet.removeAll(forceExcludeClassSet); | |
//将排除从包含列表删除 | |
includeClassSet.removeAll(excludeClassSet); | |
for (Class clazz : includeClassSet) { | |
mixMap.put(clazz, AntPathFilterMixin.class); | |
} | |
return mixMap; | |
} | |
private Set<Class<?>> buildClassSet(Set<String> packageName, Set<String> className) { | |
Set<Class<?>> classSet = new HashSet<>(); | |
if (packageName != null && !packageName.isEmpty()) { | |
packageName.stream().map(this::getSubClasses).flatMap(Collection::stream).forEach(classSet::add); | |
} | |
if (className != null && !className.isEmpty()) { | |
className.stream().map(this::getClassByName).filter(Objects::nonNull).forEach(classSet::add); | |
} | |
return classSet; | |
} | |
private Set<Class<?>> getSubClasses(String packageName) { | |
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false)); | |
return reflections.getSubTypesOf(Object.class); | |
} | |
private Class getClassByName(String className) { | |
try { | |
return Class.forName(className); | |
} catch (ClassNotFoundException e) { | |
log.warn("", e); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment