Be careful with requests and responses when using the Serverless Framework in Java

Serverless Framework / Notes on implementation in AWS / Java.

API Gateway and Lambda do not work with the code generated by the template as it is, and it does not work! !!

For example, create a project with the following command.

sls create --template aws-java-maven --path serverless

Then the following Java file is created.

package hello;



public class Handler implements RequestHandler<Request, Response> {

	public Response handleRequest(Request input, Context context) {
		return new Response("Go Serverless v1.0! Your function executed successfully!", input);


But if this method is executed through API Gateway, it won't work. Because the following JSON is passed (partially modified).

  "httpMethod" : "POST",
  "requestContext" : {
    "httpMethod" : "POST",
    "accountId" : "*******",
    "apiId" : "*******",
    "resourceId" : "******",
    "identity" : {
      "userAgent" : "Apache-HttpClient\/4.5.x (Java\/1.8.0_112)",
      "userArn" : "arn:aws:iam::******:user\/****",
      "caller" : "***************",
      "cognitoAuthenticationProvider" : null,
      "cognitoIdentityId" : null,
      "accessKey" : "******************",
      "accountId" : "*****************",
      "sourceIp" : "test-invoke-source-ip",
      "apiKey" : "test-invoke-api-key",
      "cognitoAuthenticationType" : null,
      "cognitoIdentityPoolId" : null,
      "user" : "*******************"
    "requestId" : "test-invoke-request",
    "stage" : "test-invoke-stage",
    "resourcePath" : "\/hello"
  "resource" : "\/hello",
  "pathParameters" : null,
  "isBase64Encoded" : false,
  "headers" : null,
  "path" : "\/hello",
  "stageVariables" : null,
  "queryStringParameters" : null,
  "body" : "{\n    \"key1\": \"hoge\",\n    \"key2\": 120\n}"

What I really want is the last `body` property part.

It means that you need to write a program that extracts this.

Not only the request but also the response does not work as it is. It seems that the response object needs three properties: `statusCode```, headers```, and `` body```.

~~ I will provide a sample solution to deal with this area. aws-serverless-java-sample ~~

(Fixed June 24, 2018) The AWS SDK has a corresponding solution, so I'll rewrite it.

When AWS provides a library called aws-lambda-java-events and connects Lambda to various services Request and response types are defined.

The types of requests and responses passed when connecting to API Gateway are as follows.

Since the body is a String type, the effort of JSON encode / decode remains, but it should still be much easier to code.

Recommended Posts

Be careful with requests and responses when using the Serverless Framework in Java
Be careful when using rails_semantic_logger with unicorn
Be absolutely careful when putting the result of and / or in a variable!
[Java10] Be careful of using var and generics together
Get started with serverless Java with the lightweight framework Micronaut!
Be careful of initialization timing when using MessageEncryptor with Rails 5.2 / 6.0
Syntax and exception occurrence conditions that can be used when comparing with null in Java
Be careful when using multiple articles
Install gem in Serverless Framework and AWS Lambda with Ruby environment
Be careful when omitting return in Ruby
I summarized the points to note when using resources and resources in combination
Try using the Stream API in Java
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
Try using the Wii remote with Java
How to set tabs and spaces to be visible by using the tab key to insert spaces in Java files in Eclipse
Be careful when setting the class name when creating a document-based app in Swift
Impressions and doubts about using java for the first time in Android Studio
When using a list in Java, java.awt.List comes out and an error occurs
Regarding the transient modifier and serialization in Java
Be careful if you find SHIFT-JIS in Java
ChatWork4j for using the ChatWork API in Java
Understanding the MVC framework with server-side Java 1/4 View
[Java] Be careful of the key type of Map
Understanding the MVC framework with server-side Java 3/4 Controller
Specify ClassPath when using jupyter + Java with WSL
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
Try using the COTOHA API parsing in Java
Understanding the MVC framework with server-side Java 2/4 Model
Encrypt / decrypt with AES256 in PHP and Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Summary of how to use the proxy set in IE when connecting with Java
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
Write ABNF in Java and pass the email address
How to deploy Java to AWS Lambda with Serverless Framework
[Java] Sort the list using streams and lambda expressions
Java classes and instances to understand in the figure
[Java] When var should be used and when it should not be used
How to convert A to a and a to A using AND and OR in Java
Try global hooking in Java using the JNativeHook library
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Serverless Java EE starting with Quarkus and Cloud Run
Send and receive simple messages using the Kafka component of the Apache Camel framework (Java DSL)
I was trapped when I generated my own class equals and hashCode in Java using IDE
Template creation program when using the reminder function in slack
Store in Java 2D map and turn with for statement
Implement Thread in Java and try using anonymous class, lambda
JSON in Java and Jackson Part 1 Return JSON from the server
Log aggregation and analysis (working with AWS Athena in Java)
[Java] [Play Framework] Until the project is started with Gradle
Things to be aware of when writing code in Java
Correct the character code in Java and read from the URL
Problems with Dijkstra's algorithm using PriorityQueue and adjacency list (java)
[Java] Check the difference between orElse and orElseGet with IntStream
Tips for using Salesforce SOAP and Bulk API in Java
Socket communication with a web browser using Java and JavaScript ②
About the behavior when doing a file map with java
Socket communication with a web browser using Java and JavaScript ①
[Rails] Where to be careful in the description of validation
Be sure to compare the result of Java compareTo with 0
Find the greatest common divisor and least common multiple with JAVA