GH-932 Fix registration of AWSTypesMessageConverter for functional spring applications
Resolves #932
This commit is contained in:
@@ -16,10 +16,13 @@
|
||||
|
||||
package org.springframework.cloud.function.adapter.aws;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.springframework.cloud.function.json.JacksonMapper;
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.converter.MessageConverter;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -27,11 +30,18 @@ import org.springframework.messaging.converter.MessageConverter;
|
||||
* @since 3.2
|
||||
*
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class AWSCompanionAutoConfiguration {
|
||||
public class AWSCompanionAutoConfiguration implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
|
||||
@Bean
|
||||
public MessageConverter awsTypesConverter(JsonMapper jsonMapper) {
|
||||
return new AWSTypesMessageConverter(jsonMapper);
|
||||
@Override
|
||||
public void initialize(GenericApplicationContext applicationContext) {
|
||||
applicationContext.registerBean("awsTypesMessageConverter", AWSTypesMessageConverter.class,
|
||||
() -> {
|
||||
if (CollectionUtils.isEmpty(applicationContext.getBeansOfType(JsonMapper.class).values())) {
|
||||
return new AWSTypesMessageConverter(new JacksonMapper(new ObjectMapper()));
|
||||
}
|
||||
else {
|
||||
return new AWSTypesMessageConverter(applicationContext.getBean(JsonMapper.class));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,11 +55,11 @@ class AWSTypesMessageConverter extends JsonMessageConverter {
|
||||
|
||||
@Override
|
||||
protected boolean canConvertFrom(Message<?> message, @Nullable Class<?> targetClass) {
|
||||
if (message.getHeaders().containsKey(AWSLambdaUtils.AWS_API_GATEWAY) && ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_API_GATEWAY))) {
|
||||
return true;
|
||||
if (message.getHeaders().containsKey(AWSLambdaUtils.AWS_API_GATEWAY)) {
|
||||
return ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_API_GATEWAY));
|
||||
}
|
||||
if (message.getHeaders().containsKey(AWSLambdaUtils.AWS_EVENT) && ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_EVENT))) {
|
||||
return true;
|
||||
if (message.getHeaders().containsKey(AWSLambdaUtils.AWS_EVENT)) {
|
||||
return ((boolean) message.getHeaders().get(AWSLambdaUtils.AWS_EVENT));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
org.springframework.cloud.function.adapter.aws.CustomRuntimeInitializer
|
||||
org.springframework.cloud.function.adapter.aws.CustomRuntimeInitializer,org.springframework.cloud.function.adapter.aws.AWSCompanionAutoConfiguration
|
||||
|
||||
@@ -96,6 +96,7 @@ public class FunctionalSpringApplication
|
||||
Assert.isInstanceOf(GenericApplicationContext.class, context,
|
||||
"ApplicationContext must be an instanceof GenericApplicationContext");
|
||||
for (Object source : getAllSources()) {
|
||||
System.out.println("======> SOURCE: " + source);
|
||||
Class<?> type = null;
|
||||
Object handler = null;
|
||||
if (source instanceof String) {
|
||||
|
||||
@@ -872,6 +872,7 @@ public class SimpleFunctionRegistry implements FunctionRegistry {
|
||||
if (inputValue instanceof Message && !this.isInputTypeMessage()) {
|
||||
inputValue = ((Message) inputValue).getPayload();
|
||||
}
|
||||
System.out.println("Invoking function: " + this + "with input type: " + this.getInputType());
|
||||
Object result = ((Function) this.target).apply(inputValue);
|
||||
|
||||
if (result instanceof Publisher && functionInvocationHelper != null) {
|
||||
@@ -1042,7 +1043,8 @@ public class SimpleFunctionRegistry implements FunctionRegistry {
|
||||
: convertedInput;
|
||||
}
|
||||
if (convertedInput != null && logger.isDebugEnabled()) {
|
||||
logger.debug("Converted Message: " + input + " to: " + convertedInput);
|
||||
logger.debug("Converted Message: " + input + " to: " +
|
||||
(convertedInput instanceof OriginalMessageHolder ? ((OriginalMessageHolder) convertedInput).value.getClass() : convertedInput));
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -173,6 +173,7 @@ public class ContextFunctionCatalogInitializer implements ApplicationContextInit
|
||||
List<MessageConverter> messageConverters = new ArrayList<>();
|
||||
JsonMapper jsonMapper = this.context.getBean(JsonMapper.class);
|
||||
|
||||
messageConverters.addAll(context.getBeansOfType(MessageConverter.class).values());
|
||||
messageConverters.add(new JsonMessageConverter(jsonMapper));
|
||||
messageConverters.add(new ByteArrayMessageConverter());
|
||||
messageConverters.add(new StringMessageConverter());
|
||||
|
||||
@@ -99,7 +99,13 @@ public class JsonMessageConverter extends AbstractMessageConverter {
|
||||
if (payload instanceof byte[]) {
|
||||
payload = new String((byte[]) payload, StandardCharsets.UTF_8);
|
||||
}
|
||||
logger.warn("Failed to convert value: " + payload, e);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Failed to convert value: " + payload + " to: " + targetClass, e);
|
||||
}
|
||||
else {
|
||||
logger.warn("Failed to convert value: " + payload + " to: " + targetClass);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ import org.springframework.cloud.function.context.catalog.FunctionTypeUtils;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||
|
||||
@SpringBootConfiguration
|
||||
public class FunctionConfiguration implements ApplicationContextInitializer<GenericApplicationContext> {
|
||||
|
||||
@@ -24,6 +27,15 @@ public class FunctionConfiguration implements ApplicationContextInitializer<Gene
|
||||
public void initialize(GenericApplicationContext context) {
|
||||
Function<String, String> function = (str) -> str + str.toUpperCase();
|
||||
context.registerBean("uppercase", FunctionRegistration.class,
|
||||
<<<<<<< HEAD
|
||||
() -> new FunctionRegistration<>(function).type(FunctionTypeUtils.functionType(String.class, String.class)));
|
||||
=======
|
||||
() -> new FunctionRegistration<>(function).type(
|
||||
FunctionType.from(String.class).to(String.class)));
|
||||
|
||||
context.registerBean("testFunction", FunctionRegistration.class,
|
||||
() -> new FunctionRegistration<>(new TestFunction()).type(
|
||||
FunctionType.from(APIGatewayProxyRequestEvent.class).to(APIGatewayProxyResponseEvent.class)));
|
||||
>>>>>>> 3cb9dde6... GH-932 Fix registration of AWSTypesMessageConverter for functional spring applications
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package example;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||
|
||||
public class TestFunction implements Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
|
||||
@Override
|
||||
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent) {
|
||||
return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody("ok");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user