Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Function-suitable for REST API? How to access GET path params?

I'm new to WebFlux and Serverless. I'm trying to create a REST API as Serverless via AWS API gateway.

The flow would be API Gateway --> Lambda --> DynamoDB

In order to achieve the API flow, would the Spring Cloud Function be the best choice? I found aws-serverless-java-container does the job seamlessly(wrapper of converting the event to http request/response)

I've gone through the documentation on http://cloud.spring.io/spring-cloud-function/single/spring-cloud-function.html and few examples found in https://github.com/spring-cloud/spring-cloud-function. But still, I'm not convinced on whether with Spring Cloud Function I would be able to achieve the API flavor.

@Bean
//How path or query params can be mapped?
public Function<Flux<String>, Flux<String>> getEmployeeDetails() {
 // business logic goes here
}

In the above snippet, how to achieve the GET request/response model. If my endpoint has /{dept}/{employee}/{name}, how Spring cloud function accepts the path params in GET request?

Any pointers would be helpful.

like image 820
Haran Avatar asked Jul 13 '18 12:07

Haran


2 Answers

API Gateway wraps all data of the incoming request into an APIGatewayProxyRequestEvent object, which has several attributes, like the request body, path and query parameters, or the HTTP method of the original request. For your use case, you would get the three path parameters dept, employee, and name.

However, the problem is that SpringBootApiGatewayRequestHandler, which does the mapping between the incoming APIGatewayProxyRequestEvent and your function, currently only considers a request body, but no other parameters at all.

I hope that the developers will fix this in the future. In the meantime, you could implement a modified version of SpringBootApiGatewayRequestHandler, which considers the required parameters. I've written a small article, which covers exactly this topic.

like image 50
markusgulden Avatar answered Oct 06 '22 09:10

markusgulden


I have the same mistake with this, the documentation prompts you to think that you can do this, but you can't. This is only an example from spring-cloud-function, and springboot function aws specific code doesn't have this feature implemented (as far I could see).

You can follow the default route, if you want to implement your application in lambda with spring: https://github.com/awslabs/aws-serverless-java-container/blob/master/samples/springboot/pet-store.

Or... If you want, you can try the hard way, but attemption: you are completely alone: https://github.com/arawn/building-serverless-application-with-spring-webflux.

This project himself implements ObjecMapper conversions and you can get the parameters from request: https://github.com/arawn/building-serverless-application-with-spring-webflux/blob/master/src/main/java/serverless/aws/springframework/http/server/reactive/SimpleAPIGatewayProxyServerHttpRequest.java

The trick from here is to create Path template in lambda with proxy (Path: '/{proxy+}') and request are delegated from aws mapper.

Good luck!

like image 26
Valtoni Boaventura Avatar answered Oct 06 '22 09:10

Valtoni Boaventura