diff --git a/docs/src/main/asciidoc/spring-cloud-function.adoc b/docs/src/main/asciidoc/spring-cloud-function.adoc index 6d488b6a3..530bcf335 100644 --- a/docs/src/main/asciidoc/spring-cloud-function.adoc +++ b/docs/src/main/asciidoc/spring-cloud-function.adoc @@ -424,7 +424,19 @@ public class MyCustomMessageConverter extends AbstractMessageConverter { } ---- +==== Note on JSON options +In Spring Cloud Function we support Jackson and Gson mechanisms to deal with JSON. +And for your benefit have abstracted it under `org.springframework.cloud.function.json.JsonMapper` which itself is aware of two mechanisms and will use the one selected +by you or following the default rule. +The default rules are as follows: + +* Whichever library is on the classpath that is the mechanism that is going to be used. So if you have `com.fasterxml.jackson.*` to the classpath, Jackson is going to be used and if you have `com.google.code.gson`, then Gson will be used. +* If you have both, then Gson will be the default, or you can set `spring.cloud.function.preferred-json-mapper` property with either of two values: `gson` or `jackson`. + + +That said, the type conversion is usually transparent to the developer, however given that `org.springframework.cloud.function.json.JsonMapper` is also registered as a bean +you can easily inject it into your code if needed. === Kotlin Lambda support diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java index 9f013a936..8b3be3d1e 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogAutoConfiguration.java @@ -76,8 +76,11 @@ import org.springframework.util.StringUtils; @EnableConfigurationProperties(FunctionProperties.class) public class ContextFunctionCatalogAutoConfiguration { + @Deprecated static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper"; + static final String JSON_MAPPER_PROPERTY = "spring.cloud.function.preferred-json-mapper"; + @Bean public FunctionRegistry functionCatalog(List messageConverters, JsonMapper jsonMapper, ConfigurableApplicationContext context, @Nullable FunctionInvocationHelper> functionInvocationHelper) { @@ -154,7 +157,9 @@ public class ContextFunctionCatalogAutoConfiguration { public static class JsonMapperConfiguration { @Bean public JsonMapper jsonMapper(ApplicationContext context) { - String preferredMapper = context.getEnvironment().getProperty(PREFERRED_MAPPER_PROPERTY); + String preferredMapper = context.getEnvironment().containsProperty(JSON_MAPPER_PROPERTY) + ? context.getEnvironment().getProperty(JSON_MAPPER_PROPERTY) + : context.getEnvironment().getProperty(PREFERRED_MAPPER_PROPERTY); if (StringUtils.hasText(preferredMapper)) { if ("gson".equals(preferredMapper) && ClassUtils.isPresent("com.google.gson.Gson", null)) { return gson(context); diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogInitializer.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogInitializer.java index 87d014f33..109a97c38 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogInitializer.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/config/ContextFunctionCatalogInitializer.java @@ -132,8 +132,13 @@ public class ContextFunctionCatalogInitializer implements ApplicationContextInit AnnotationConfigUtils.registerAnnotationConfigProcessors(this.context); } ConfigurationPropertiesBindingPostProcessor.register(registry); - if (ClassUtils.isPresent("com.google.gson.Gson", null) && "gson".equals(this.context.getEnvironment() - .getProperty(ContextFunctionCatalogAutoConfiguration.PREFERRED_MAPPER_PROPERTY, "gson"))) { + + String preferredMapper = context.getEnvironment().containsProperty(ContextFunctionCatalogAutoConfiguration.JSON_MAPPER_PROPERTY) + ? context.getEnvironment().getProperty(ContextFunctionCatalogAutoConfiguration.JSON_MAPPER_PROPERTY) + : context.getEnvironment().getProperty(ContextFunctionCatalogAutoConfiguration.PREFERRED_MAPPER_PROPERTY); + + + if (ClassUtils.isPresent("com.google.gson.Gson", null) && "gson".equals(preferredMapper)) { if (this.context.getBeanFactory().getBeanNamesForType(Gson.class, false, false).length == 0) { this.context.registerBean(Gson.class, () -> new Gson()); }