API Gateway ins't created ok in Java project


A newbie question for the Java-version of serverless (doing the same for NodeJS works fine).

I just created a test project in Java with serverless, but it doesn’t work.
I get this as a response:
{“message”: “Internal server error”}

I created/built/deployed the project using the following steps:

serverless create --template aws-java-maven

I changed the serverless.yml, removing the # for the following rows:

  - http:
      path: users/create
      method: get

I then built & deployed the application:

mvn install
serverless deploy

What seems to be the problem is that the API gatway is created as a LAMBDA_PROXY, which doesn’t work.

If I create a new GET function (NOT selecting Use Lambda Proxy integration) in the AWS console for the API and points it to the created Lambda everything works great.

Any ideas on how to get this to work directly after serverless deploy ?

// Mikael

1 Like

I have the exact same problem and can also only resolve it by adding integration: lambda to the serverless.yml file.

Any insights would be greatly appreciated!

The Java templates (Gradle and maven) aren’t designed for Lambda Proxy Integration. If you want to make them work with Lambda Proxy Integration you have to return a response object like the JavaScript template does. This means you have to return an object with getters for statusCode (int), body (String) and headers (Map<String, String ) - a response object could look like this: https://github.com/bbilger/jrestless/blob/master/aws/gateway/jrestless-aws-gateway-handler/src/main/java/com/jrestless/aws/gateway/io/GatewayResponse.java.
The body must be a string! So make sure to serialize your actual response body to a string - if you want a JSON response you can use Jackson for serialization, for example.

You might want to check out my framework jrestless: https://github.com/bbilger/jrestless. It’s a Jersey container for AWS Lambda and provides JAX-RS functionality through Jersey - the reference implementation for JAX-RS. This allows usage of most JAX-RS features and gives Spring integration, too. You can use serverless to configure and deploy your Lambda functions.

Thanks for the hint, I could get it to work now. jrestless looks great!

FYI: Since it wasn’t mentioned in the release notes: serverless v1.5.0 has updated Java templates that work with lambda-proxy.