GH-567 Add KotlinModule when present to jacksonObjectMapperBuilder

Resolves #567
This commit is contained in:
Oleg Zhurakousky
2021-09-27 17:01:35 +02:00
parent 63a5678f26
commit ebec01f21b
2 changed files with 29 additions and 0 deletions

View File

@@ -22,6 +22,7 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import com.fasterxml.jackson.module.kotlin.KotlinModule;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
@@ -45,13 +46,18 @@ import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.ObjectUtils;
/**
* Configuration class which defines the required infrastructure to bootstrap Kotlin
* lambdas as invocable functions within the context of the framework.
@@ -67,6 +73,19 @@ public class KotlinLambdaToFunctionAutoConfiguration {
protected final Log logger = LogFactory.getLog(getClass());
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = {"org.springframework.http.converter.json.Jackson2ObjectMapperBuilder",
"com.fasterxml.jackson.module.kotlin.KotlinModule"})
Jackson2ObjectMapperBuilderCustomizer customizer() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.modulesToInstall(KotlinModule.class);
}
};
}
/**
* Will transform all discovered Kotlin's Function lambdas to java
* Supplier, Function and Consumer, retaining the original Kotlin type