GH-677 Add spring.cloud.function.preferred-json-mapper property

Deprecate spring.http.converters.preferred-json-mapper
Resolves #677
This commit is contained in:
Oleg Zhurakousky
2021-04-09 15:50:50 +02:00
parent bbcec718ce
commit a4c1358af4
3 changed files with 25 additions and 3 deletions

View File

@@ -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

View File

@@ -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<MessageConverter> messageConverters, JsonMapper jsonMapper,
ConfigurableApplicationContext context, @Nullable FunctionInvocationHelper<Message<?>> 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);

View File

@@ -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());
}