If I use AWS API gateway and integration type set to lambda function, in the Java implementation of the lambda function, is it possible to override RequestHandler
(instead of RequestStreamHandler
)? So that the input to my lambda function will be a POJO I defined in Java code and output of it would be another POJO and the client should just need to send the JSON serialization of that request POJO and receives a JSON serialization of the response POJO. I've tried to do that but whatever my client sends it will just receive an empty 200 response.
Not sure what's going wrong here, can anyone help?
And yes, you can call this API (Lambda proxy) as any Rest API.
You can now use an AWS Lambda function from a different AWS account as your API integration backend. Each account can be in any region where Amazon API Gateway is available. This makes it easy to centrally manage and share Lambda backend functions across multiple APIs.
So I create a sample app
LambdaFunctionHandler.java
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaFunctionHandler implements RequestHandler<RequestClass, ResponseClass> {
public ResponseClass handleRequest(RequestClass input, Context context) {
context.getLogger().log("Input: " + input);
// TODO: implement your handler
ResponseClass myVal = new ResponseClass();
myVal.setGreetings("Hello from POJO - " + input.getName() );
return myVal;
}
}
ResponseClass.java
package com.amazonaws.lambda.demo;
public class ResponseClass {
String greetings;
public String getGreetings() {
return greetings;
}
public void setGreetings(String greetings) {
this.greetings = greetings;
}
public ResponseClass(String greetings) {
this.greetings = greetings;
}
public ResponseClass() {
}
}
RequestClass.java
package com.amazonaws.lambda.demo;
public class RequestClass {
String name;
public String getName() {
return name;
}
public void setName(String firstName) {
this.name = firstName;
}
public RequestClass(String name) {
this.name = name;
}
public RequestClass() {
}
}
After uploading the same to AWS, I created a API Gateway for the same
And the tests on the same
As you can see POJO works in Lambda + API Gateway combination
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With