Skip to content

Commit 66ede7f

Browse files
markusguldenKevinGilmore
authored andcommitted
Improvements (eugenp#4610)
1 parent 5515220 commit 66ede7f

4 files changed

Lines changed: 22 additions & 127 deletions

File tree

aws-lambda/sam-templates/template-implicit.yaml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ Resources:
3333
Properties:
3434
Path: /persons
3535
Method: PUT
36-
GetPersonByPathParamFunction:
36+
GetPersonByHTTPParamFunction:
3737
Type: AWS::Serverless::Function
3838
Properties:
39-
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByPathParam
39+
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
4040
Runtime: java8
4141
Timeout: 15
4242
MemorySize: 512
@@ -53,21 +53,6 @@ Resources:
5353
Properties:
5454
Path: /persons/{id}
5555
Method: GET
56-
GetPersonByQueryParamFunction:
57-
Type: AWS::Serverless::Function
58-
Properties:
59-
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByQueryParam
60-
Runtime: java8
61-
Timeout: 15
62-
MemorySize: 512
63-
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
64-
Policies:
65-
- DynamoDBReadPolicy:
66-
TableName: !Ref PersonTable
67-
Environment:
68-
Variables:
69-
TABLE_NAME: !Ref PersonTable
70-
Events:
7156
GetByQueryApi:
7257
Type: Api
7358
Properties:

aws-lambda/sam-templates/template-inline-swagger.yaml

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ Resources:
3131
Method: PUT
3232
RestApiId:
3333
Ref: MyApi
34-
GetPersonByPathParamFunction:
34+
GetPersonByHTTPParamFunction:
3535
Type: AWS::Serverless::Function
3636
Properties:
37-
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByPathParam
37+
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
3838
Runtime: java8
3939
Timeout: 15
4040
MemorySize: 512
@@ -53,21 +53,6 @@ Resources:
5353
Method: GET
5454
RestApiId:
5555
Ref: MyApi
56-
GetPersonByQueryParamFunction:
57-
Type: AWS::Serverless::Function
58-
Properties:
59-
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByQueryParam
60-
Runtime: java8
61-
Timeout: 15
62-
MemorySize: 512
63-
CodeUri: ../target/aws-lambda-0.1.0-SNAPSHOT.jar
64-
Policies:
65-
- DynamoDBReadPolicy:
66-
TableName: !Ref PersonTable
67-
Environment:
68-
Variables:
69-
TABLE_NAME: !Ref PersonTable
70-
Events:
7156
GetByQueryApi:
7257
Type: Api
7358
Properties:
@@ -96,7 +81,7 @@ Resources:
9681
\ headers"
9782
x-amazon-apigateway-integration:
9883
uri:
99-
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByQueryParamFunction.Arn}/invocations
84+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByHTTPParamFunction.Arn}/invocations
10085
responses: {}
10186
httpMethod: "POST"
10287
type: "aws_proxy"
@@ -117,7 +102,7 @@ Resources:
117102
responses: {}
118103
x-amazon-apigateway-integration:
119104
uri:
120-
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByPathParamFunction.Arn}/invocations
105+
Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetPersonByHTTPParamFunction.Arn}/invocations
121106
responses: {}
122107
httpMethod: "POST"
123108
type: "aws_proxy"

aws-lambda/src/main/java/com/baeldung/lambda/apigateway/APIDemoHandler.java

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
public class APIDemoHandler implements RequestStreamHandler {
1717

1818
private JSONParser parser = new JSONParser();
19-
private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME");
19+
private static final String DYNAMODB_TABLE_NAME = System.getenv("TABLE_NAME");
2020

2121
@Override
2222
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
@@ -35,10 +35,8 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
3535
Person person = new Person((String) event.get("body"));
3636

3737
dynamoDb.getTable(DYNAMODB_TABLE_NAME)
38-
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
39-
.withString("firstName", person.getFirstName())
40-
.withString("lastName", person.getLastName()).withNumber("age", person.getAge())
41-
.withString("address", person.getAddress())));
38+
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
39+
.withString("name", person.getName())));
4240
}
4341

4442
JSONObject responseBody = new JSONObject();
@@ -61,8 +59,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
6159
writer.close();
6260
}
6361

64-
public void handleGetByPathParam(InputStream inputStream, OutputStream outputStream, Context context)
65-
throws IOException {
62+
public void handleGetByParam(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
6663

6764
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
6865
JSONObject responseJson = new JSONObject();
@@ -81,61 +78,20 @@ public void handleGetByPathParam(InputStream inputStream, OutputStream outputStr
8178
if (pps.get("id") != null) {
8279

8380
int id = Integer.parseInt((String) pps.get("id"));
84-
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id);
81+
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME)
82+
.getItem("id", id);
8583
}
8684

87-
}
88-
if (result != null) {
89-
90-
Person person = new Person(result.toJSON());
91-
responseBody.put("Person", person);
92-
responseJson.put("statusCode", 200);
93-
} else {
94-
95-
responseBody.put("message", "No item found");
96-
responseJson.put("statusCode", 404);
97-
}
98-
99-
JSONObject headerJson = new JSONObject();
100-
headerJson.put("x-custom-header", "my custom header value");
101-
102-
responseJson.put("headers", headerJson);
103-
responseJson.put("body", responseBody.toString());
104-
105-
} catch (ParseException pex) {
106-
responseJson.put("statusCode", 400);
107-
responseJson.put("exception", pex);
108-
}
109-
110-
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
111-
writer.write(responseJson.toString());
112-
writer.close();
113-
}
114-
115-
public void handleGetByQueryParam(InputStream inputStream, OutputStream outputStream, Context context)
116-
throws IOException {
117-
118-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
119-
JSONObject responseJson = new JSONObject();
120-
121-
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
122-
DynamoDB dynamoDb = new DynamoDB(client);
123-
124-
Item result = null;
125-
try {
126-
JSONObject event = (JSONObject) parser.parse(reader);
127-
JSONObject responseBody = new JSONObject();
128-
129-
if (event.get("queryStringParameters") != null) {
85+
} else if (event.get("queryStringParameters") != null) {
13086

13187
JSONObject qps = (JSONObject) event.get("queryStringParameters");
13288
if (qps.get("id") != null) {
13389

13490
int id = Integer.parseInt((String) qps.get("id"));
135-
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME).getItem("id", id);
91+
result = dynamoDb.getTable(DYNAMODB_TABLE_NAME)
92+
.getItem("id", id);
13693
}
13794
}
138-
13995
if (result != null) {
14096

14197
Person person = new Person(result.toJSON());
@@ -145,7 +101,7 @@ public void handleGetByQueryParam(InputStream inputStream, OutputStream outputSt
145101

146102
responseBody.put("message", "No item found");
147103
responseJson.put("statusCode", 404);
148-
}
104+
}
149105

150106
JSONObject headerJson = new JSONObject();
151107
headerJson.put("x-custom-header", "my custom header value");
@@ -162,5 +118,4 @@ public void handleGetByQueryParam(InputStream inputStream, OutputStream outputSt
162118
writer.write(responseJson.toString());
163119
writer.close();
164120
}
165-
166121
}

aws-lambda/src/main/java/com/baeldung/lambda/apigateway/model/Person.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@
66
public class Person {
77

88
private int id;
9-
private String firstName;
10-
private String lastName;
11-
private int age;
12-
private String address;
9+
private String name;
1310

1411
public Person(String json) {
1512
Gson gson = new Gson();
1613
Person request = gson.fromJson(json, Person.class);
1714
this.id = request.getId();
18-
this.firstName = request.getFirstName();
19-
this.lastName = request.getLastName();
20-
this.age = request.getAge();
21-
this.address = request.getAddress();
15+
this.name = request.getName();
2216
}
2317

2418
public String toString() {
@@ -34,35 +28,11 @@ public void setId(int id) {
3428
this.id = id;
3529
}
3630

37-
public String getFirstName() {
38-
return firstName;
31+
public String getName() {
32+
return name;
3933
}
4034

41-
public void setFirstName(String firstName) {
42-
this.firstName = firstName;
35+
public void setName(String name) {
36+
this.name = name;
4337
}
44-
45-
public String getLastName() {
46-
return lastName;
47-
}
48-
49-
public void setLastName(String lastName) {
50-
this.lastName = lastName;
51-
}
52-
53-
public int getAge() {
54-
return age;
55-
}
56-
57-
public void setAge(int age) {
58-
this.age = age;
59-
}
60-
61-
public String getAddress() {
62-
return address;
63-
}
64-
65-
public void setAddress(String address) {
66-
this.address = address;
67-
}
6838
}

0 commit comments

Comments
 (0)