GH-587 Add support for inferring 'accept' content type for simple types

This fix also introduces new Function property 'accept' with no default value which implicitely would default to application/json unless the output type of the function is String at which point it would default to text/plain. However, if it was explicitely set in FunctionProperties it will be used regardless of the function output type.
Resolves #587
This commit is contained in:
Oleg Zhurakousky
2020-09-16 18:14:40 +02:00
parent e1adb011ab
commit d3afd1fea4
7 changed files with 86 additions and 50 deletions

View File

@@ -44,10 +44,7 @@ public class FunctionProperties {
private String definition;
private String contentType = "application/json";
private String accept = "application/json";
private String accept;
/**
* SpEL expression which should result in function definition (e.g., function name or composition instruction).
@@ -71,14 +68,6 @@ public class FunctionProperties {
this.routingExpression = routingExpression;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getAccept() {
return accept;
}

View File

@@ -49,7 +49,6 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.core.type.StandardMethodMetadata;
import org.springframework.lang.Nullable;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -85,20 +84,19 @@ public class BeanFactoryAwareFunctionRegistry extends SimpleFunctionRegistry imp
this.applicationContext.getBeanNamesForType(Consumer.class).length;
}
@SuppressWarnings("unchecked")
@Override
public Set<String> getNames(Class<?> type) {
Set<String> registeredNames = super.getNames(type);
if (type == null) {
registeredNames
.addAll(CollectionUtils.arrayToList(this.applicationContext.getBeanNamesForType(Function.class)));
.addAll(Arrays.asList(this.applicationContext.getBeanNamesForType(Function.class)));
registeredNames
.addAll(CollectionUtils.arrayToList(this.applicationContext.getBeanNamesForType(Supplier.class)));
.addAll(Arrays.asList(this.applicationContext.getBeanNamesForType(Supplier.class)));
registeredNames
.addAll(CollectionUtils.arrayToList(this.applicationContext.getBeanNamesForType(Consumer.class)));
.addAll(Arrays.asList(this.applicationContext.getBeanNamesForType(Consumer.class)));
}
else {
registeredNames.addAll(CollectionUtils.arrayToList(this.applicationContext.getBeanNamesForType(type)));
registeredNames.addAll(Arrays.asList(this.applicationContext.getBeanNamesForType(type)));
}
return registeredNames;
}

View File

@@ -20,6 +20,7 @@ package org.springframework.cloud.function.context.catalog;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -56,7 +57,6 @@ import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeType;
import org.springframework.util.ReflectionUtils;
@@ -532,13 +532,13 @@ public class BeanFactoryAwareFunctionRegistryTests {
@Bean
public Function<String, List<String>> parseToList() {
return v -> CollectionUtils.arrayToList(v.split(","));
return v -> Arrays.asList(v.split(","));
}
@Bean
public Function<String, List<Message<String>>> parseToListOfMessages() {
return v -> {
List<Message<String>> list = (List<Message<String>>) CollectionUtils.arrayToList(v.split(",")).stream()
List<Message<String>> list = Arrays.asList(v.split(",")).stream()
.map(value -> MessageBuilder.withPayload(value).build()).collect(Collectors.toList());
return list;
};