-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,189 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
This file contains 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. |
52 changes: 52 additions & 0 deletions
52
samples/custom-serialization/fastJson/HelloWorldFunction/pom.xml
This file contains 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> |
50 changes: 50 additions & 0 deletions
50
...lloWorldFunction/src/main/java/com/example/vehicles/serialization/FastJsonSerializer.java
This file contains 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); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
samples/custom-serialization/fastJson/HelloWorldFunction/src/main/java/helloworld/App.java
This file contains 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); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...es/custom-serialization/fastJson/HelloWorldFunction/src/main/java/helloworld/Vehicle.java
This file contains 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 + '\'' + | ||
'}'; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/services/com.amazonaws.services.lambda.runtime.CustomPojoSerializer
This file contains 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 |
This file contains 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 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 | ||
} |
This file contains 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 |
51 changes: 51 additions & 0 deletions
51
samples/custom-serialization/gson/HelloWorldFunction/pom.xml
This file contains 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> |
Oops, something went wrong.