FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

sample code for custom-serialization by maxday · Pull Request #513 · aws/aws-lambda-java-libs · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .CustomPojoSerializer  (4) .gradle  (1) .java  (14) .json  (5) .md  (6) .xml  (4) .yaml  (5) .yml  (1) dotfile  (1) All 9 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
    • .gitignore
    • README.md
        • pom.xml
              • FastJsonSerializer.java
              • App.java
              • Vehicle.java
      • README.md
        • event.json
      • template.yaml
        • pom.xml
              • GsonSerializer.java
              • App.java
              • Vehicle.java
      • README.md
        • event.json
      • template.yaml
        • build.gradle
              • JacksonJRSerializer.java
              • App.java
              • Vehicle.java
      • README.md
        • event.json
      • template.yaml
        • pom.xml
              • MoshiSerializer.java
              • App.java
              • Vehicle.java
      • README.md
        • event.json
      • template.yaml
        • pom.xml
          • App.java
          • Vehicle.java
      • README.md
        • event.json
      • template.yaml
31 changes: 26 additions & 5 deletions .github/workflows/samples.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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);
}
}

}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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);
}

}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 + '\'' +
'}';
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
```

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL