How to get path parameter in spring cloud function

997 views Asked by At

I have a java application written using spring-cloud-functions and deployed in aws lambda which connects to aws api-gateway

I have a 'GET' api(let's say /employees/{employeeId}) which takes an input parameter({employeeId} in this case) and returns the result(employee details for the given employeeId) corresponding to the parameter.

I tried googling but could not find a way to set this up such that I get the {employeeId} passed to my spring-cloud-function code and act accordingly. How do I get the parameter in the java code?

Can anyone suggest how to set this up in api-gateway and get the same parameter in my java lambda code?

Thanks.

2

There are 2 answers

0
Vasco On

I used REST API with lambda integration. We need to convert the path params and query params to an event object that the lambda can consume.

So here goes, in integration tab:

URL Query String Parameters

Mapping Templates

You can refer here for details: AWS Knowledge Centre Article

Same logic can be applied to path params as well.

0
Shubam Virdi On

We can try to do workaround as for any type of request we will getting uri of the request.

Extracting the uri from the headers provided the input for spring cloud function should be of type of Message or any other which accepts headers

@Bean
public Function<Message<Void>, Message<?>> api(){}

String uri = (String) request.getHeaders().get("uri");

For eq:
Uri: /post/12345

Uri Template can be used to extract the parameters in the string

private HashMap<String, String> getPathParameters(String uri, String strUrlTemplate) {
        UriTemplate template = new UriTemplate(strUrlTemplate);
        Map<String, String> parameters = new HashMap<>();
        return new HashMap<>(template.match(uri));
    }

The function accepts uri and the template for that which will be in this case /post/{id}

which will return a map

{
    "id":"12345"
}