【发布时间】:2017-08-18 12:20:29
【问题描述】:
对于部署在 AWS lambda 上的 restful 应用程序,我在自定义 API 网关域时遇到问题。自定义域以这种方式工作,根据 basePath 选择不同的 API,最终触及 Lambda。例如:
api.mycustomdomain.com/view/ping -> 使用路径/view/ping 转到应用程序view
api.mycustomdomain.com/admin/ping -> 使用路径 /admin/ping 转到应用程序 admin
我将此示例用作样板:https://github.com/awslabs/aws-serverless-java-container/tree/master/samples/spring/pet-store
我想要实现的是依赖于 Host 标头从请求路径中去除前缀的处理程序。
我准备了以下application.yml文件:
server:
contextPath: "/view"
productionHost: "api.mycustomdomain.com"
问题/问题是。我现在如何将它们加载到我的 Lambda 函数中?这是我天真的尝试:
public class LambdaHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse> {
SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
boolean isinitialized = false;
@Value("${server.contextPath}")
private String prefix;
@Value("${server.productionHost}")
private String productionHost;
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) {
if(awsProxyRequest.getHeaders().get("Host").equals(productionHost))
awsProxyRequest.setPath(awsProxyRequest.getPath().substring(prefix.length()));
if (!isinitialized) {
isinitialized = true;
try {
handler = SpringLambdaContainerHandler.getAwsProxyHandler(PingPongApp.class);
} catch (ContainerInitializationException e) {
e.printStackTrace();
return null;
}
}
return handler.proxy(awsProxyRequest, context);
}
}
显然这不起作用,LambdaHandler 正在脱离 Spring 上下文。
任何想法我该如何处理?
【问题讨论】:
标签: java spring amazon-web-services aws-lambda microservices