Skip to content

Instantly share code, notes, and snippets.

@richardgrantserverless
Created April 11, 2022 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardgrantserverless/884bc236a5ad8f8bb32793b1af547ae3 to your computer and use it in GitHub Desktop.
Save richardgrantserverless/884bc236a5ad8f8bb32793b1af547ae3 to your computer and use it in GitHub Desktop.
package com.serverless;
...
import com.serverless.dal.Product;
public class GetProductHandler implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
private final Logger logger = Logger.getLogger(this.getClass());
@Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
try {
// get the 'pathParameters' from input
Map<String,String> pathParameters = (Map<String,String>)input.get("pathParameters");
String productId = pathParameters.get("id");
// get the Product by id
Product product = new Product().get(productId);
// send the response back
if (product != null) {
return ApiGatewayResponse.builder()
.setStatusCode(200)
.setObjectBody(product)
.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & Serverless"))
.build();
} else {
return ApiGatewayResponse.builder()
.setStatusCode(404)
.setObjectBody("Product with id: '" + productId + "' not found.")
.setHeaders(Collections.singletonMap("X-Powered-By", "AWS Lambda & Serverless"))
.build();
}
} catch (Exception ex) {
logger.error("Error in retrieving product: " + ex);
// send the error response back
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment