我正在回答我自己的问题。最后,我确认使用 JAX-RS 客户端可以按预期与 API 网关一起工作。就我而言,我更喜欢 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 Gateway 公开的 REST 端点,我不需要使用 AWS 控制台生成的庞大 Java 库。