Get Mapping Template from API Gateway as JSON in AWS Lambda Java Project Get Mapping Template from API Gateway as JSON in AWS Lambda Java Project json json

Get Mapping Template from API Gateway as JSON in AWS Lambda Java Project


Thanks to Ka Hou leong for the direction, I've solved with the following:

1) Added dependency in Maven for aws-serverless-java-container

<dependency>    <groupId>com.amazonaws.serverless</groupId>    <artifactId>aws-serverless-java-container-core</artifactId>    <version>0.4</version></dependency>

2) Modified my LambdaFunctionHandler class to use AwsProxyRequest and AwsProxyResponse:

import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;import com.amazonaws.services.lambda.runtime.Context;import com.amazonaws.services.lambda.runtime.RequestHandler;public class LambdaFunctionHandler implements RequestHandler<AwsProxyRequest, Object> {    public Object handleRequest(AwsProxyRequest input, Context context) {        AwsProxyResponse response = new AwsProxyResponse();        String resourcePath = input.getRequestContext().getResourcePath();        response.setStatusCode(200);        response.setBody(resourcePath);    return response;    }}

3) Modified my API Gateway method Integration Request settings to Use Lambda Proxy Integration.

From here I have access to everything in the input object as an AwsProxyRequest, and will manipulate response as an AwsProxyResponse with anything I want to respond with.


You can write your own POJO to match the request from API Gateway, then you will be able to access the resource path from a getter.

POJOs example:

public class LambdaFunctionHandler implements RequestHandler<Object, Object> {    @Override    public Object handleRequest(AwsProxyRequest input, Context context) {        String resourcePath = input.getRequestContext().getResourcePath();        return resourcePath;    }}public class AwsProxyRequest {    //-------------------------------------------------------------    // Variables - Private    //-------------------------------------------------------------    private ApiGatewayRequestContext requestContext;    ....    //-------------------------------------------------------------    // Methods - Getter/Setter    //-------------------------------------------------------------    public ApiGatewayRequestContext getRequestContext() {        return requestContext;    }    public void setRequestContext(ApiGatewayRequestContext requestContext) {        this.requestContext = requestContext;    }    ....}public class ApiGatewayRequestContext {    //-------------------------------------------------------------    // Variables - Private    //-------------------------------------------------------------    private String resourcePath;    ...    //-------------------------------------------------------------    // Methods - Getter/Setter    //-------------------------------------------------------------    public String getResourcePath() {        return resourcePath;    }    public void setResourcePath(String resourcePath) {        this.resourcePath = resourcePath;    }    ....}

If you want the full proxy request POJO, you can find them from here.