AWS Lambda在Java中:如何调用在API网关中公开的REST端点



我代表API网关在AWS基础设施上公开了一个REST API。在Java中的Lambda函数中,我需要调用相关联的端点。AWS文档建议使用API网关控制台生成客户端。然而,生成的客户端有几十个类,甚至可能有100个!当然,不可能是这样。

是否有人成功地使用RESTeasy或CXF客户端调用了API网关公开的端点?甚至Apache HTTP客户端?

非常感谢。

我在回答我自己的问题。最后,我确认使用JAX-RS客户端与API网关的预期一样工作。在我的情况下,我更喜欢RESTeasy,但它也应该与CXF一起使用,甚至与ApacheHTTPClient一起使用。以下是我所做的:

Maven依赖项:

...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-bom</artifactId>
<version>4.4.1.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
...
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>4.4.1.Final</version>
<exclusions>
<exclusion>
<groupId>org.jboss.spec.javax.xml.bind</groupId>
<artifactId>jboss-jaxb-api_2.3_spec</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<exclusions>
<exclusion>
<groupId>org.jboss.spec.javax.xml.bind</groupId>
<artifactId>jboss-jaxb-api_2.3_spec</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta.activation</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
</dependency>
...

请注意,排除是为了解决maven shade插件的问题,它会重载相同工件的不同版本。

maven shade插件配置:

...
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>...</finalName>
<createDependencyReducedPom>false</createDependencyReducedPom>
<!-- Using filtering in order to get rid of nasty warnings generated by shading module-info-->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
</excludes>
</filter>
</filters>
<!-- Required as per https://stackoverflow.com/questions/24023155-->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</plugin>
...

Java客户端代码:

...
private static ResteasyClient client =  new ResteasyClientBuilderImpl().httpEngine(new URLConnectionEngine()).build();
private static WebTarget webTarget = client.target("https://...");
...
webTarget.request().post(Entity.entity(..., "application/json"));
...

请注意,使用JAX-RS纯客户端如下:

...
private static Client client = ClientBuilder.newClient();
...

将不起作用,并将引发以下异常:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7933f1c0: Failure in SSL library, usually a protocol error

因此,为了解决这个问题,我需要使用RESTeasy(非JAX-RS(特定的东西,这是不好的。这是一个RESTeasy错误,本应在4.5中解决。然而,由于一些弃用和重构(类ResteasyClientBuilderImpl已经不存在了,等等(,我更喜欢使用4.4。

因此,像预期的那样工作,Lambda函数成功地调用了通过API网关公开的REST端点,我不需要使用AWS控制台生成的巨大Java库。

最新更新