Java AWS Lambda+;Spring,如何加载应用程序.yml

Java AWS Lambda+;Spring,如何加载应用程序.yml,java,spring,amazon-web-services,aws-lambda,microservices,Java,Spring,Amazon Web Services,Aws Lambda,Microservices,我在为部署在AWS lambda上的restful应用自定义API网关域时遇到问题。自定义域的工作方式是,根据basePath,它选择不同的API,最终触及Lambda。例如: api.mycustomdomain.com/view/ping->进入应用程序view,路径为/view/ping api.mycustomdomain.com/admin/ping->进入应用程序admin,路径为/admin/ping 我将此示例用作样板: 我想要实现的是处理程序,它根据Host头从请求路径中剥离前

我在为部署在AWS lambda上的restful应用自定义API网关域时遇到问题。自定义域的工作方式是,根据basePath,它选择不同的API,最终触及Lambda。例如:

api.mycustomdomain.com/view/ping
->进入应用程序
view
,路径为
/view/ping
api.mycustomdomain.com/admin/ping
->进入应用程序
admin
,路径为
/admin/ping

我将此示例用作样板:

我想要实现的是处理程序,它根据
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实现RequestHandler{
SpringLambdaContainerHandler处理器;
布尔isinitialized=false;
@值(${server.contextPath}”)
私有字符串前缀;
@值(${server.productionHost}”)
私有字符串productionHost;
公共AwsProxyResponse HandlerRequest(AwsProxyRequest AwsProxyRequest,上下文){
if(awsProxyRequest.getHeaders().get(“主机”).equals(productionHost))
setPath(awsProxyRequest.getPath().substring(prefix.length());
如果(!i初始化){
isinitialized=true;
试一试{
handler=SpringLambdaContainerHandler.getAwsProxyHandler(PingPongApp.class);
}捕获(容器初始化异常e){
e、 printStackTrace();
返回null;
}
}
代理(awsProxyRequest,上下文);
}
}
显然这是行不通的,LambdaHandler是在Spring环境下工作的


你知道我该怎么处理吗?

看来你无法加载这些属性。遵循下面给出的两个选项之一

1> 您可以在配置中添加以下bean,这样您就可以自动连接字符串并使用您已经使用的方式

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
2>


您似乎无法加载这些属性。遵循下面给出的两个选项之一

1> 您可以在配置中添加以下bean,这样您就可以自动连接字符串并使用您已经使用的方式

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
2>


我试过第一种选择。它不起作用。我认为原因是,执行流程是:1。api获取请求->传递给lambda(LambdaHandler)->2。Lambda处理程序正在调用Spring,所以我无法访问LambdaHandler中的@Value(此时Spring尚未初始化)。我不明白您在第二个建议中的观点。你想让我覆盖AwsProxyResponse?为什么?不,我只是说了另一种检索属性的方法。声明env变量(org.springframework.core.env.Environoment),因为此处加载属性时出错。我尝试了第一个选项。它不起作用。我认为原因是,执行流程是:1。api获取请求->传递给lambda(LambdaHandler)->2。Lambda处理程序正在调用Spring,所以我无法访问LambdaHandler中的@Value(此时Spring尚未初始化)。我不明白您在第二个建议中的观点。你想让我覆盖AwsProxyResponse?为什么?不,我只是说了另一种检索属性的方法。声明env变量(org.springframework.core.env.Environoment),因为此处加载属性时出错。