Improvements (#4610)

This commit is contained in:
markusgulden 2018-07-04 05:22:46 +02:00 committed by KevinGilmore
parent 5515220308
commit 66ede7ffcc
4 changed files with 22 additions and 127 deletions

View File

@ -33,10 +33,10 @@ Resources:
Properties:
Path: /persons
Method: PUT
GetPersonByPathParamFunction:
GetPersonByHTTPParamFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByPathParam
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
Runtime: java8
Timeout: 15
MemorySize: 512
@ -53,21 +53,6 @@ Resources:
Properties:
Path: /persons/{id}
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:
Type: Api
Properties:

View File

@ -31,10 +31,10 @@ Resources:
Method: PUT
RestApiId:
Ref: MyApi
GetPersonByPathParamFunction:
GetPersonByHTTPParamFunction:
Type: AWS::Serverless::Function
Properties:
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByPathParam
Handler: com.baeldung.lambda.apigateway.APIDemoHandler::handleGetByParam
Runtime: java8
Timeout: 15
MemorySize: 512
@ -53,21 +53,6 @@ Resources:
Method: GET
RestApiId:
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:
Type: Api
Properties:
@ -96,7 +81,7 @@ Resources:
\ headers"
x-amazon-apigateway-integration:
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: {}
httpMethod: "POST"
type: "aws_proxy"
@ -117,7 +102,7 @@ Resources:
responses: {}
x-amazon-apigateway-integration:
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: {}
httpMethod: "POST"
type: "aws_proxy"

View File

@ -16,7 +16,7 @@ import java.io.*;
public class APIDemoHandler implements RequestStreamHandler {
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
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"));
dynamoDb.getTable(DYNAMODB_TABLE_NAME)
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
.withString("firstName", person.getFirstName())
.withString("lastName", person.getLastName()).withNumber("age", person.getAge())
.withString("address", person.getAddress())));
.putItem(new PutItemSpec().withItem(new Item().withNumber("id", person.getId())
.withString("name", person.getName())));
}
JSONObject responseBody = new JSONObject();
@ -61,8 +59,7 @@ public class APIDemoHandler implements RequestStreamHandler {
writer.close();
}
public void handleGetByPathParam(InputStream inputStream, OutputStream outputStream, Context context)
throws IOException {
public void handleGetByParam(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
JSONObject responseJson = new JSONObject();
@ -81,61 +78,20 @@ public class APIDemoHandler implements RequestStreamHandler {
if (pps.get("id") != null) {
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);
}
}
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) {
} else if (event.get("queryStringParameters") != null) {
JSONObject qps = (JSONObject) event.get("queryStringParameters");
if (qps.get("id") != null) {
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) {
Person person = new Person(result.toJSON());
@ -145,7 +101,7 @@ public class APIDemoHandler implements RequestStreamHandler {
responseBody.put("message", "No item found");
responseJson.put("statusCode", 404);
}
}
JSONObject headerJson = new JSONObject();
headerJson.put("x-custom-header", "my custom header value");
@ -162,5 +118,4 @@ public class APIDemoHandler implements RequestStreamHandler {
writer.write(responseJson.toString());
writer.close();
}
}

View File

@ -6,19 +6,13 @@ import com.google.gson.GsonBuilder;
public class Person {
private int id;
private String firstName;
private String lastName;
private int age;
private String address;
private String name;
public Person(String json) {
Gson gson = new Gson();
Person request = gson.fromJson(json, Person.class);
this.id = request.getId();
this.firstName = request.getFirstName();
this.lastName = request.getLastName();
this.age = request.getAge();
this.address = request.getAddress();
this.name = request.getName();
}
public String toString() {
@ -34,35 +28,11 @@ public class Person {
this.id = id;
}
public String getFirstName() {
return firstName;
public String getName() {
return name;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
public void setName(String name) {
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;
}
}