为 Jersey 资源提供 lambda 上下文
Supply lambda context to Jersey resource
我正在使用 aws-serverless-java-container 在 AWS Lambda 中包装 Jersey 服务。我决定使用函数别名来获得 "test" 和 "prod" 阶段,最终指向不同版本的 lambda 函数。
我需要 select 基于该别名的 .properties 文件中的某些属性,主要是因为我需要与 "test" 或 "prod" 数据库通信或使用不同的端点调用外部网络服务。
为此,我需要调用 Context object 的方法 getInvokedFunctionArn。不幸的是,Jersey 资源在默认情况下并不知道这种上下文。
示例资源下方:
@Path("/pet")
public class PetResource {
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
public Response createPet() {
// how to call getInvokedFunctionArn from Lambda context object?
return Response.status(200).entity("{'result': 'success'}").build();
}
}
如何使 Jersey 资源具有调用的 ARN?可以注射吗?
lambda 处理程序定义为:
public class PetLambdaHandler implements RequestStreamHandler {
private static final ResourceConfig jerseyApplication = new ResourceConfig().register(PetResource.class)
.register(JacksonFeature.class);
private static final JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = JerseyLambdaContainerHandler
.getAwsProxyHandler(jerseyApplication);
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}
您可以使用
将上下文注入球衣资源
为测试和生产配置文件定义不同的 methods/objects
如果没有Spring,可以查看jersey integration
jersey-spring4 module that will act as the integration bridge for Spring and Jersey.
请参阅 github aws-serverless-java-container 库中的完整示例 运行 具有 AWS Lambda
中配置文件的 Spring 应用程序
There are two ways to activate Spring Profiles (as defined with the @Profile annotation). We recommend using the static initializer that receives a list of profiles. The Serverless Java Container framework takes care of setting the profile and initializing the application at once.
public class StreamLambdaHandler implements RequestStreamHandler {
private static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
static {
try {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(PetStoreSpringAppConfig.class);
handler = SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext, "profile-1", "profile-2");
您无法自动注入 Lambda 上下文。但是,您可以从 ContainerRequestContext
对象中检索它。 serverless-java-container
framework adds it as a request property.
你或许可以这样做(没有测试代码):
@GET
public String testLambdaContext(@Context ContainerRequestContext containerRequest) {
Context lambdaContext =
(Context) containerRequest.getProperty(RequestReader.LAMBDA_CONTEXT_PROPERTY);
return lambdaContext.getInvokedFunctionArn()
}
我正在使用 aws-serverless-java-container 在 AWS Lambda 中包装 Jersey 服务。我决定使用函数别名来获得 "test" 和 "prod" 阶段,最终指向不同版本的 lambda 函数。
我需要 select 基于该别名的 .properties 文件中的某些属性,主要是因为我需要与 "test" 或 "prod" 数据库通信或使用不同的端点调用外部网络服务。
为此,我需要调用 Context object 的方法 getInvokedFunctionArn。不幸的是,Jersey 资源在默认情况下并不知道这种上下文。
示例资源下方:
@Path("/pet")
public class PetResource {
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
public Response createPet() {
// how to call getInvokedFunctionArn from Lambda context object?
return Response.status(200).entity("{'result': 'success'}").build();
}
}
如何使 Jersey 资源具有调用的 ARN?可以注射吗?
lambda 处理程序定义为:
public class PetLambdaHandler implements RequestStreamHandler {
private static final ResourceConfig jerseyApplication = new ResourceConfig().register(PetResource.class)
.register(JacksonFeature.class);
private static final JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = JerseyLambdaContainerHandler
.getAwsProxyHandler(jerseyApplication);
@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
handler.proxyStream(inputStream, outputStream, context);
}
}
您可以使用
为测试和生产配置文件定义不同的 methods/objects
如果没有Spring,可以查看jersey integration
jersey-spring4 module that will act as the integration bridge for Spring and Jersey.
请参阅 github aws-serverless-java-container 库中的完整示例 运行 具有 AWS Lambda
中配置文件的 Spring 应用程序There are two ways to activate Spring Profiles (as defined with the @Profile annotation). We recommend using the static initializer that receives a list of profiles. The Serverless Java Container framework takes care of setting the profile and initializing the application at once.
public class StreamLambdaHandler implements RequestStreamHandler { private static SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler; static { try { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(PetStoreSpringAppConfig.class); handler = SpringLambdaContainerHandler.getAwsProxyHandler(applicationContext, "profile-1", "profile-2");
您无法自动注入 Lambda 上下文。但是,您可以从 ContainerRequestContext
对象中检索它。 serverless-java-container
framework adds it as a request property.
你或许可以这样做(没有测试代码):
@GET
public String testLambdaContext(@Context ContainerRequestContext containerRequest) {
Context lambdaContext =
(Context) containerRequest.getProperty(RequestReader.LAMBDA_CONTEXT_PROPERTY);
return lambdaContext.getInvokedFunctionArn()
}