[英]Can an AWS lambda handler be created in Kotlin/Java without implementing RequestHandler?
TLDR: In CDK, a the lambda handler is a method reference, so for Java/Kotlin is it required that you implement the RequestHandler class? TLDR:在 CDK 中,lambda 处理程序是一个方法引用,因此对于 Java/Kotlin,您是否需要实现 RequestHandler class?
The typical approach for defining a lambda in Java/Kotlin is to extend the request handler class and override the handleRequest function.在 Java/Kotlin 中定义 lambda 的典型方法是扩展请求处理程序 class 并覆盖 handleRequest function。 However, when you have many similar functions in Java, this results in many similar files/class definitions.
但是,当您在 Java 中有许多类似的功能时,这会导致许多类似的文件/类定义。
The recommended definition looks like this:推荐的定义如下所示:
public class Handler implements RequestHandler<Event, String>{
@Override
public String handleRequest(Event event, Context context)
{
...
}
}
And the simplest I can come up with in Kotlin is naturally not a significant improvement.而我在Kotlin中能想出的最简单的,自然不会有明显的提升。 It looks like this:
它看起来像这样:
class Handler : RequestHandler<Event, String> {
override fun handleRequest(request: Event, context: Context) : String {
...
}
}
In CDK, a the lambda handler is a method reference, so is the "RequestHandler" implementation necessary?在 CDK 中,lambda 处理程序是方法引用,那么“RequestHandler”实现是否必要? This is really the key question, putting this at the top.
这确实是关键问题,把它放在首位。
In Java the simplest Lambda handler looks like:在 Java 中,最简单的 Lambda 处理程序如下所示:
public class SampleHandler implements RequestStreamHandler {
public void handleRequest(InputStream inputStream,
OutputStream outputStream,
Context context) throws IOException {
context.getLogger().log("in the Lambda handler");
}
}
within the handleRequest
method you will have to read from the inputStream
.在
handleRequest
方法中,您必须从inputStream
中读取。 One example could be:一个例子可能是:
ObjectMapper mapper = new ObjectMapper();
YourObject yourObject = mapper.readValue(inputStream, YourObject.class);
Now you have the object that Lambda would normally deserialize for you.现在您有了 Lambda 通常会为您反序列化的 object。 To "return" something you just need to write to the
outputStream
:要“返回”某些内容,您只需要写入
outputStream
:
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
writer.write("{\"result\":\"everything is awesome\"}");
writer.close();
Of course you could also use something like mapper.writeValue(outputStream, yourResultObject)
to not do the JSON serialization yourself.当然,您也可以使用类似
mapper.writeValue(outputStream, yourResultObject)
的东西来自己不进行 JSON 序列化。
You don't have to implement the RequestHandler
interface.您不必实现
RequestHandler
接口。 According to the official documentation , you just need to tell AWS which function to invoke if your class doesn't implement RequestHandler
interface:根据 官方文档,如果您的 class 没有实现
RequestHandler
接口,您只需告诉 AWS 要调用哪个 function:
example.Handler::handleRequest
There's nothing special about defining such a function in a CDK application:在 CDK 应用程序中定义这样的 function 并没有什么特别之处:
Function handlerFunction = Function.Builder.create(this, "ExampleHandlerFunction")
.functionName("example-handler-function")
.handler("example.Handler::handleRequest")
.runtime(Runtime.JAVA_11)
.code(Code.fromAsset("path/to/the/jar"))
.build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.