Skip to content

Commit

Permalink
Merge branch 'main' into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan authored Nov 22, 2024
2 parents 83d0ad7 + dc6e7f6 commit 081e646
Show file tree
Hide file tree
Showing 41 changed files with 1,189 additions and 5 deletions.
31 changes: 26 additions & 5 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ on:
push:
branches: [ main ]
paths:
- 'samples/kinesis-firehose-event-handler/**'
- 'samples/**'
pull_request:
branches: [ '*' ]
paths:
- 'samples/kinesis-firehose-event-handler/**'
- 'samples/**'
- '.github/workflows/samples.yml'

jobs:
build:

build-kinesis-sample:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK 1.8
Expand All @@ -37,3 +35,26 @@ jobs:
# Install samples
- name: Install Kinesis Firehose Sample with Maven
run: mvn -B install --file samples/kinesis-firehose-event-handler/pom.xml

custom-serialization:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: 21
distribution: corretto
# Build custom-serialization samples
- name: install sam
uses: aws-actions/setup-sam@v2
- name: test fastJson
run: cd samples/custom-serialization/fastJson && sam build && sam local invoke -e events/event.json | grep 200
- name: test gson
run: cd samples/custom-serialization/gson && sam build && sam local invoke -e events/event.json | grep 200
- name: test jackson-jr
run: cd samples/custom-serialization/jackson-jr && sam build && sam local invoke -e events/event.json | grep 200
- name: test moshi
run: cd samples/custom-serialization/moshi && sam build && sam local invoke -e events/event.json | grep 200
- name: test request-stream-handler
run: cd samples/custom-serialization/request-stream-handler && sam build && sam local invoke -e events/event.json | grep 200

7 changes: 7 additions & 0 deletions samples/custom-serialization/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
**/target/
**/HelloWorld.iml
**/samconfig.toml
**/dependency-reduced-pom.xml
**/.aws-sam
**/.gradle
**/bin
5 changes: 5 additions & 0 deletions samples/custom-serialization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The Lambda Java managed runtimes support custom serialization for JSON events.
https://docs.aws.amazon.com/lambda/latest/dg/java-custom-serialization.html

## Sample projects
In this repository you will find a number of sample projects from AWS to help you get started with the custom serialization feature.
52 changes: 52 additions & 0 deletions samples/custom-serialization/fastJson/HelloWorldFunction/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>helloworld</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>A sample Hello World created for SAM CLI.</name>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.14.0</version>
</dependency>

<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.33</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.vehicles.serialization;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.amazonaws.services.lambda.runtime.CustomPojoSerializer;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;

public class FastJsonSerializer implements CustomPojoSerializer {
/**
* ServiceLoader class requires that the single exposed provider type has a default constructor
* to easily instantiate the service providers that it finds
*/
public FastJsonSerializer() {
}

@Override
public <T> T fromJson(InputStream input, Type type) {
try {
return JSON.parseObject(input, type);
} catch (JSONException e) {
throw (e);
}
}

@Override
public <T> T fromJson(String input, Type type) {
try {
return JSON.parseObject(input, type);
} catch (JSONException e) {
throw (e);
}
}

@Override
public <T> void toJson(T value, OutputStream output, Type type) {
try {
JSON.writeTo(output, value);
} catch (JSONException e) {
throw (e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package helloworld;

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

/**
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<Vehicle, APIGatewayProxyResponseEvent> {

public APIGatewayProxyResponseEvent handleRequest(Vehicle vehicle, Context context) {
System.out.println("input: " + vehicle);

return new APIGatewayProxyResponseEvent().withStatusCode(200);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package helloworld;

import com.alibaba.fastjson2.annotation.JSONField;

public class Vehicle {

@JSONField(name = "vehicle-type")
private String vehicleType;

@JSONField(name = "vehicleID")
private String vehicleId;

public Vehicle() {
}

public Vehicle(String vehicleType, String vehicleId) {
this.vehicleType = vehicleType;
this.vehicleId = vehicleId;
}

public String getVehicleType() {
return vehicleType;
}

public void setVehicleType(String vehicleType) {
this.vehicleType = vehicleType;
}

public String getVehicleId() {
return vehicleId;
}

public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}

@Override
public String toString() {
return "Vehicle{" +
"vehicleType='" + vehicleType + '\'' +
", vehicleId='" + vehicleId + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.example.vehicles.serialization.FastJsonSerializer
7 changes: 7 additions & 0 deletions samples/custom-serialization/fastJson/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Build and test commands

```bash
sam build
sam local invoke -e events/event.json
```

4 changes: 4 additions & 0 deletions samples/custom-serialization/fastJson/events/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"vehicle-type": "car",
"vehicleID": 123
}
43 changes: 43 additions & 0 deletions samples/custom-serialization/fastJson/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
fastJson
Sample SAM Template for fastJson
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 20
MemorySize: 512

Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java21
Architectures:
- x86_64
MemorySize: 512
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello
Method: get

Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
51 changes: 51 additions & 0 deletions samples/custom-serialization/gson/HelloWorldFunction/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>helloworld</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>A sample Hello World created for SAM CLI.</name>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 081e646

Please sign in to comment.