简体   繁体   中英

Aws Lambda: How to get query params from Api Gateway in lambda function implemented in Java

Aws Lambda - How to get query params from Api Gateway in lambda function implemented in Java.

I have following code snippet :

package com.amazonaws.lambda.demo;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, String> {

    @Override
    public String handleRequest(String input, Context context) {
        context.getLogger().log("Input: " + input);

        // TODO: implement your handler
        return "Hello from " + input;
    }
}

How can i access query params if any in above handleRequest function.

in pom.xml make sure " 2.2.5 " or newer version is specified here

<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-events</artifactId> <version>2.2.5</version> </dependency>

Your java lambda class:

import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class LambdaFunctionHandler
    implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input,
            Context context) {
        Map<String, String> inputParams = input.getPathParameters();

        APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
        responseEvent.setBody("body text");
        responseEvent.setStatusCode(200);
        return responseEvent;
    }
}

You can try mapping everything you want in integration request body mapping template of API Gateway. Once you construct body mapping template then in the context of lambda you will get the excat json you have constructed.

Please find below link, which I have given a solution for similar kind of question,

https://stackoverflow.com/a/46407780/7666972

Based on tgk23's answer above, I believe you are looking for the query parameters, not the path parameters:

public class LambdaFunctionHandler
        implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        Map<String, String> inputParams = input.getQueryStringParameters();
        for (Map.Entry<String, String> entry : inputParams.entrySet()) {
            LOGGER.info("param: " + entry.getKey() + "=" + entry.getValue());
        }

        APIGatewayProxyResponseEvent responseEvent = new APIGatewayProxyResponseEvent();
        responseEvent.setBody("body text");
        responseEvent.setStatusCode(200);
        return responseEvent;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM