| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,18 +7,16 @@ on: | |||
| 7 | 7 | push: | |
| 8 | 8 | branches: [ main ] | |
| 9 | 9 | paths: | |
| 10 | - - 'samples/kinesis-firehose-event-handler/**' | ||
| 10 | + - 'samples/**' | ||
| 11 | 11 | pull_request: | |
| 12 | 12 | branches: [ '*' ] | |
| 13 | 13 | paths: | |
| 14 | - - 'samples/kinesis-firehose-event-handler/**' | ||
| 14 | + - 'samples/**' | ||
| 15 | 15 | - '.github/workflows/samples.yml' | |
| 16 | 16 | ||
| 17 | 17 | jobs: | |
| 18 | - build: | ||
| 19 | - | ||
| 18 | + build-kinesis-sample: | ||
| 20 | 19 | runs-on: ubuntu-latest | |
| 21 | - | ||
| 22 | 20 | steps: | |
| 23 | 21 | - uses: actions/checkout@v4 | |
| 24 | 22 | - name: Set up JDK 1.8 | |
@@ -37,3 +35,26 @@ jobs: | |||
| 37 | 35 | # Install samples | |
| 38 | 36 | - name: Install Kinesis Firehose Sample with Maven | |
| 39 | 37 | run: mvn -B install --file samples/kinesis-firehose-event-handler/pom.xml | |
| 38 | + | ||
| 39 | + custom-serialization: | ||
| 40 | + runs-on: ubuntu-latest | ||
| 41 | + steps: | ||
| 42 | + - uses: actions/checkout@v4 | ||
| 43 | + - uses: actions/setup-java@v4 | ||
| 44 | + with: | ||
| 45 | + java-version: 21 | ||
| 46 | + distribution: corretto | ||
| 47 | + # Build custom-serialization samples | ||
| 48 | + - name: install sam | ||
| 49 | + uses: aws-actions/setup-sam@v2 | ||
| 50 | + - name: test fastJson | ||
| 51 | + run: cd samples/custom-serialization/fastJson && sam build && sam local invoke -e events/event.json | grep 200 | ||
| 52 | + - name: test gson | ||
| 53 | + run: cd samples/custom-serialization/gson && sam build && sam local invoke -e events/event.json | grep 200 | ||
| 54 | + - name: test jackson-jr | ||
| 55 | + run: cd samples/custom-serialization/jackson-jr && sam build && sam local invoke -e events/event.json | grep 200 | ||
| 56 | + - name: test moshi | ||
| 57 | + run: cd samples/custom-serialization/moshi && sam build && sam local invoke -e events/event.json | grep 200 | ||
| 58 | + - name: test request-stream-handler | ||
| 59 | + run: cd samples/custom-serialization/request-stream-handler && sam build && sam local invoke -e events/event.json | grep 200 | ||
| 60 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + **/target/ | ||
| 2 | + **/HelloWorld.iml | ||
| 3 | + **/samconfig.toml | ||
| 4 | + **/dependency-reduced-pom.xml | ||
| 5 | + **/.aws-sam | ||
| 6 | + **/.gradle | ||
| 7 | + **/bin | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + The Lambda Java managed runtimes support custom serialization for JSON events. | ||
| 2 | + https://docs.aws.amazon.com/lambda/latest/dg/java-custom-serialization.html | ||
| 3 | + | ||
| 4 | + ## Sample projects | ||
| 5 | + In this repository you will find a number of sample projects from AWS to help you get started with the custom serialization feature. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,52 @@ | |||
| 1 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| 3 | + <modelVersion>4.0.0</modelVersion> | ||
| 4 | + <groupId>helloworld</groupId> | ||
| 5 | + <artifactId>HelloWorld</artifactId> | ||
| 6 | + <version>1.0</version> | ||
| 7 | + <packaging>jar</packaging> | ||
| 8 | + <name>A sample Hello World created for SAM CLI.</name> | ||
| 9 | + <properties> | ||
| 10 | + <maven.compiler.source>21</maven.compiler.source> | ||
| 11 | + <maven.compiler.target>21</maven.compiler.target> | ||
| 12 | + </properties> | ||
| 13 | + | ||
| 14 | + <dependencies> | ||
| 15 | + <dependency> | ||
| 16 | + <groupId>com.amazonaws</groupId> | ||
| 17 | + <artifactId>aws-lambda-java-core</artifactId> | ||
| 18 | + <version>1.2.3</version> | ||
| 19 | + </dependency> | ||
| 20 | + <dependency> | ||
| 21 | + <groupId>com.amazonaws</groupId> | ||
| 22 | + <artifactId>aws-lambda-java-events</artifactId> | ||
| 23 | + <version>3.14.0</version> | ||
| 24 | + </dependency> | ||
| 25 | + | ||
| 26 | + <dependency> | ||
| 27 | + <groupId>com.alibaba.fastjson2</groupId> | ||
| 28 | + <artifactId>fastjson2</artifactId> | ||
| 29 | + <version>2.0.33</version> | ||
| 30 | + </dependency> | ||
| 31 | + </dependencies> | ||
| 32 | + | ||
| 33 | + <build> | ||
| 34 | + <plugins> | ||
| 35 | + <plugin> | ||
| 36 | + <groupId>org.apache.maven.plugins</groupId> | ||
| 37 | + <artifactId>maven-shade-plugin</artifactId> | ||
| 38 | + <version>3.2.4</version> | ||
| 39 | + <configuration> | ||
| 40 | + </configuration> | ||
| 41 | + <executions> | ||
| 42 | + <execution> | ||
| 43 | + <phase>package</phase> | ||
| 44 | + <goals> | ||
| 45 | + <goal>shade</goal> | ||
| 46 | + </goals> | ||
| 47 | + </execution> | ||
| 48 | + </executions> | ||
| 49 | + </plugin> | ||
| 50 | + </plugins> | ||
| 51 | + </build> | ||
| 52 | + </project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,50 @@ | |||
| 1 | + /* | ||
| 2 | + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| 3 | + SPDX-License-Identifier: Apache-2.0 | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | + package com.example.vehicles.serialization; | ||
| 7 | + | ||
| 8 | + import com.alibaba.fastjson2.JSON; | ||
| 9 | + import com.alibaba.fastjson2.JSONException; | ||
| 10 | + import com.amazonaws.services.lambda.runtime.CustomPojoSerializer; | ||
| 11 | + import java.io.InputStream; | ||
| 12 | + import java.io.OutputStream; | ||
| 13 | + import java.lang.reflect.Type; | ||
| 14 | + | ||
| 15 | + public class FastJsonSerializer implements CustomPojoSerializer { | ||
| 16 | + /** | ||
| 17 | + * ServiceLoader class requires that the single exposed provider type has a default constructor | ||
| 18 | + * to easily instantiate the service providers that it finds | ||
| 19 | + */ | ||
| 20 | + public FastJsonSerializer() { | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + @Override | ||
| 24 | + public <T> T fromJson(InputStream input, Type type) { | ||
| 25 | + try { | ||
| 26 | + return JSON.parseObject(input, type); | ||
| 27 | + } catch (JSONException e) { | ||
| 28 | + throw (e); | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + @Override | ||
| 33 | + public <T> T fromJson(String input, Type type) { | ||
| 34 | + try { | ||
| 35 | + return JSON.parseObject(input, type); | ||
| 36 | + } catch (JSONException e) { | ||
| 37 | + throw (e); | ||
| 38 | + } | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + @Override | ||
| 42 | + public <T> void toJson(T value, OutputStream output, Type type) { | ||
| 43 | + try { | ||
| 44 | + JSON.writeTo(output, value); | ||
| 45 | + } catch (JSONException e) { | ||
| 46 | + throw (e); | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + /* | ||
| 2 | + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| 3 | + SPDX-License-Identifier: Apache-2.0 | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | + package helloworld; | ||
| 7 | + | ||
| 8 | + import com.amazonaws.services.lambda.runtime.Context; | ||
| 9 | + import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
| 10 | + import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
| 11 | + | ||
| 12 | + /** | ||
| 13 | + * Handler for requests to Lambda function. | ||
| 14 | + */ | ||
| 15 | + public class App implements RequestHandler<Vehicle, APIGatewayProxyResponseEvent> { | ||
| 16 | + | ||
| 17 | + public APIGatewayProxyResponseEvent handleRequest(Vehicle vehicle, Context context) { | ||
| 18 | + System.out.println("input: " + vehicle); | ||
| 19 | + | ||
| 20 | + return new APIGatewayProxyResponseEvent().withStatusCode(200); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,49 @@ | |||
| 1 | + /* | ||
| 2 | + Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| 3 | + SPDX-License-Identifier: Apache-2.0 | ||
| 4 | + */ | ||
| 5 | + | ||
| 6 | + package helloworld; | ||
| 7 | + | ||
| 8 | + import com.alibaba.fastjson2.annotation.JSONField; | ||
| 9 | + | ||
| 10 | + public class Vehicle { | ||
| 11 | + | ||
| 12 | + @JSONField(name = "vehicle-type") | ||
| 13 | + private String vehicleType; | ||
| 14 | + | ||
| 15 | + @JSONField(name = "vehicleID") | ||
| 16 | + private String vehicleId; | ||
| 17 | + | ||
| 18 | + public Vehicle() { | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public Vehicle(String vehicleType, String vehicleId) { | ||
| 22 | + this.vehicleType = vehicleType; | ||
| 23 | + this.vehicleId = vehicleId; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public String getVehicleType() { | ||
| 27 | + return vehicleType; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public void setVehicleType(String vehicleType) { | ||
| 31 | + this.vehicleType = vehicleType; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public String getVehicleId() { | ||
| 35 | + return vehicleId; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public void setVehicleId(String vehicleId) { | ||
| 39 | + this.vehicleId = vehicleId; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + @Override | ||
| 43 | + public String toString() { | ||
| 44 | + return "Vehicle{" + | ||
| 45 | + "vehicleType='" + vehicleType + '\'' + | ||
| 46 | + ", vehicleId='" + vehicleId + '\'' + | ||
| 47 | + '}'; | ||
| 48 | + } | ||
| 49 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + com.example.vehicles.serialization.FastJsonSerializer | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + Build and test commands | ||
| 2 | + | ||
| 3 | + ```bash | ||
| 4 | + sam build | ||
| 5 | + sam local invoke -e events/event.json | ||
| 6 | + ``` | ||
| 7 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,4 @@ | |||
| 1 | + { | ||
| 2 | + "vehicle-type": "car", | ||
| 3 | + "vehicleID": 123 | ||
| 4 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments