如何解决Java中的AWS Lambda:如何调用API Gateway中公开的REST端点
我代表API Gateway在AWS基础设施上公开了REST API。在Java中的Lambda函数中,我需要调用关联的端点。 AWS文档建议使用API Gateway控制台生成客户端。但是,生成的客户端具有几十个类,甚至可能是100个!当然,这不可能。
有人使用RESTeasy或CXF客户端成功调用了API网关公开的终结点吗?甚至Apache HTTP客户端?
非常感谢。
解决方法
我在回答自己的问题。最后,我确认使用JAX-RS客户端可以与API Gateway一起使用。就我而言,我更喜欢RESTeasy,但它也应与CXF甚至与Apache HTTp客户端一起使用。这是我所做的:
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-plugin的排除项将使相同工件的不同版本超载。
maven-shade-plugin配置:
...
<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库。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。