Improvements (#4610)
This commit is contained in:
parent
5515220308
commit
66ede7ffcc
|
@ -33,10 +33,10 @@ Resources:
|
||||||
Properties:
|
Properties:
|
||||||
Path: /persons
|
Path: /persons
|
||||||
Method: PUT
|
Method: PUT
|
||||||
GetPersonByPathParamFunction:
|
GetPersonByHTTPParamFunction:
|
||||||
Type: AWS::Serverless::Function
|
Type: AWS::Serverless::Function
|
||||||
Properties:
|
Properties:
|
||||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByPathParam
|
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
|
||||||
Runtime: java8
|
Runtime: java8
|
||||||
Timeout: 15
|
Timeout: 15
|
||||||
MemorySize: 512
|
MemorySize: 512
|
||||||
|
@ -53,21 +53,6 @@ Resources:
|
||||||
Properties:
|
Properties:
|
||||||
Path: /persons/{id}
|
Path: /persons/{id}
|
||||||
Method: GET
|
Method: GET
|
||||||
GetPersonByQueryParamFunction:
|
|
||||||
Type: AWS::Serverless::Function
|
|
||||||
Properties:
|
|
||||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByQueryParam
|
|
||||||
Runtime: java8
|
|
||||||
Timeout: 15
|
|
||||||
MemorySize: 512
|
|
||||||
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
|
|
||||||
Policies:
|
|
||||||
- DynamoDBReadPolicy:
|
|
||||||
TableName: !Ref PersonTable
|
|
||||||
Environment:
|
|
||||||
Variables:
|
|
||||||
TABLE_NAME: !Ref PersonTable
|
|
||||||
Events:
|
|
||||||
GetByQueryApi:
|
GetByQueryApi:
|
||||||
Type: Api
|
Type: Api
|
||||||
Properties:
|
Properties:
|
||||||
|
|
|
@ -31,10 +31,10 @@ Resources:
|
||||||
Method: PUT
|
Method: PUT
|
||||||
RestApiId:
|
RestApiId:
|
||||||
Ref: MyApi
|
Ref: MyApi
|
||||||
GetPersonByPathParamFunction:
|
GetPersonByHTTPParamFunction:
|
||||||
Type: AWS::Serverless::Function
|
Type: AWS::Serverless::Function
|
||||||
Properties:
|
Properties:
|
||||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByPathParam
|
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
|
||||||
Runtime: java8
|
Runtime: java8
|
||||||
Timeout: 15
|
Timeout: 15
|
||||||
MemorySize: 512
|
MemorySize: 512
|
||||||
|
@ -53,21 +53,6 @@ Resources:
|
||||||
Method: GET
|
Method: GET
|
||||||
RestApiId:
|
RestApiId:
|
||||||
Ref: MyApi
|
Ref: MyApi
|
||||||
GetPersonByQueryParamFunction:
|
|
||||||
Type: AWS::Serverless::Function
|
|
||||||
Properties:
|
|
||||||
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByQueryParam
|
|
||||||
Runtime: java8
|
|
||||||
Timeout: 15
|
|
||||||
MemorySize: 512
|
|
||||||
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
|
|
||||||
Policies:
|
|
||||||
- DynamoDBReadPolicy:
|
|
||||||
TableName: !Ref PersonTable
|
|
||||||
Environment:
|
|
||||||
Variables:
|
|
||||||
TABLE_NAME: !Ref PersonTable
|
|
||||||
Events:
|
|
||||||
GetByQueryApi:
|
GetByQueryApi:
|
||||||
Type: Api
|
Type: Api
|
||||||
Properties:
|
Properties:
|
||||||
|
@ -96,7 +81,7 @@ Resources:
|
||||||
\ headers"
|
\ headers"
|
||||||
x-amazon-apigateway-integration:
|
x-amazon-apigateway-integration:
|
||||||
uri:
|
uri:
|
||||||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByQueryParamFunction.Arn}/invocations
|
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByHTTPParamFunction.Arn}/invocations
|
||||||
responses: {}
|
responses: {}
|
||||||
httpMethod: "POST"
|
httpMethod: "POST"
|
||||||
type: "aws_proxy"
|
type: "aws_proxy"
|
||||||
|
@ -117,7 +102,7 @@ Resources:
|
||||||
responses: {}
|
responses: {}
|
||||||
x-amazon-apigateway-integration:
|
x-amazon-apigateway-integration:
|
||||||
uri:
|
uri:
|
||||||
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByPathParamFunction.Arn}/invocations
|
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByHTTPParamFunction.Arn}/invocations
|
||||||
responses: {}
|
responses: {}
|
||||||
httpMethod: "POST"
|
httpMethod: "POST"
|
||||||
type: "aws_proxy"
|
type: "aws_proxy"
|
||||||
|
|
|
@ -16,7 +16,7 @@ import java.io.*;
|
||||||
public class APIDemoHandler implements RequestStreamHandler {
|
public class APIDemoHandler implements RequestStreamHandler {
|
||||||
|
|
||||||
private JSONParser parser = new JSONParser();
|
private JSONParser parser = new JSONParser();
|
||||||
private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME");
|
private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||||
|
@ -35,10 +35,8 @@ public class APIDemoHandler implements RequestStreamHandler {
|
||||||
Person person = new Person((String) event.get("body"));
|
Person person = new Person((String) event.get("body"));
|
||||||
|
|
||||||
dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||||
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
|
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
|
||||||
.withString("firstName", person.getFirstName())
|
.withString("name", person.getName())));
|
||||||
.withString("lastName", person.getLastName()).withNumber("age", person.getAge())
|
|
||||||
.withString("address", person.getAddress())));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject responseBody = new JSONObject();
|
JSONObject responseBody = new JSONObject();
|
||||||
|
@ -61,8 +59,7 @@ public class APIDemoHandler implements RequestStreamHandler {
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleGetByPathParam(InputStream inputStream, OutputStream outputStream, Context context)
|
public void handleGetByParam(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
JSONObject responseJson = new JSONObject();
|
JSONObject responseJson = new JSONObject();
|
||||||
|
@ -81,61 +78,20 @@ public class APIDemoHandler implements RequestStreamHandler {
|
||||||
if (pps.get("id") != null) {
|
if (pps.get("id") != null) {
|
||||||
|
|
||||||
int id = Integer.parseInt((String) pps.get("id"));
|
int id = Integer.parseInt((String) pps.get("id"));
|
||||||
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id);
|
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||||
|
.getItem("id", id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} else if (event.get("queryStringParameters") != null) {
|
||||||
if (result != null) {
|
|
||||||
|
|
||||||
Person person = new Person(result.toJSON());
|
|
||||||
responseBody.put("Person", person);
|
|
||||||
responseJson.put("statusCode", 200);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
responseBody.put("message", "No item found");
|
|
||||||
responseJson.put("statusCode", 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONObject headerJson = new JSONObject();
|
|
||||||
headerJson.put("x-custom-header", "my custom header value");
|
|
||||||
|
|
||||||
responseJson.put("headers", headerJson);
|
|
||||||
responseJson.put("body", responseBody.toString());
|
|
||||||
|
|
||||||
} catch (ParseException pex) {
|
|
||||||
responseJson.put("statusCode", 400);
|
|
||||||
responseJson.put("exception", pex);
|
|
||||||
}
|
|
||||||
|
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
|
|
||||||
writer.write(responseJson.toString());
|
|
||||||
writer.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleGetByQueryParam(InputStream inputStream, OutputStream outputStream, Context context)
|
|
||||||
throws IOException {
|
|
||||||
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
||||||
JSONObject responseJson = new JSONObject();
|
|
||||||
|
|
||||||
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
|
|
||||||
DynamoDB dynamoDb = new DynamoDB(client);
|
|
||||||
|
|
||||||
Item result = null;
|
|
||||||
try {
|
|
||||||
JSONObject event = (JSONObject) parser.parse(reader);
|
|
||||||
JSONObject responseBody = new JSONObject();
|
|
||||||
|
|
||||||
if (event.get("queryStringParameters") != null) {
|
|
||||||
|
|
||||||
JSONObject qps = (JSONObject) event.get("queryStringParameters");
|
JSONObject qps = (JSONObject) event.get("queryStringParameters");
|
||||||
if (qps.get("id") != null) {
|
if (qps.get("id") != null) {
|
||||||
|
|
||||||
int id = Integer.parseInt((String) qps.get("id"));
|
int id = Integer.parseInt((String) qps.get("id"));
|
||||||
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id);
|
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME)
|
||||||
|
.getItem("id", id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
|
|
||||||
Person person = new Person(result.toJSON());
|
Person person = new Person(result.toJSON());
|
||||||
|
@ -145,7 +101,7 @@ public class APIDemoHandler implements RequestStreamHandler {
|
||||||
|
|
||||||
responseBody.put("message", "No item found");
|
responseBody.put("message", "No item found");
|
||||||
responseJson.put("statusCode", 404);
|
responseJson.put("statusCode", 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONObject headerJson = new JSONObject();
|
JSONObject headerJson = new JSONObject();
|
||||||
headerJson.put("x-custom-header", "my custom header value");
|
headerJson.put("x-custom-header", "my custom header value");
|
||||||
|
@ -162,5 +118,4 @@ public class APIDemoHandler implements RequestStreamHandler {
|
||||||
writer.write(responseJson.toString());
|
writer.write(responseJson.toString());
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,19 +6,13 @@ import com.google.gson.GsonBuilder;
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
private int id;
|
private int id;
|
||||||
private String firstName;
|
private String name;
|
||||||
private String lastName;
|
|
||||||
private int age;
|
|
||||||
private String address;
|
|
||||||
|
|
||||||
public Person(String json) {
|
public Person(String json) {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
Person request = gson.fromJson(json, Person.class);
|
Person request = gson.fromJson(json, Person.class);
|
||||||
this.id = request.getId();
|
this.id = request.getId();
|
||||||
this.firstName = request.getFirstName();
|
this.name = request.getName();
|
||||||
this.lastName = request.getLastName();
|
|
||||||
this.age = request.getAge();
|
|
||||||
this.address = request.getAddress();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -34,35 +28,11 @@ public class Person {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getName() {
|
||||||
return firstName;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
public void setName(String name) {
|
||||||
this.firstName = firstName;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(int age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAddress() {
|
|
||||||
return address;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAddress(String address) {
|
|
||||||
this.address = address;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue