java - 如何在ReSTLet的ServerResource方法中请求另一个api作为客户端

标签 java restlet restlet-2.0 restlet-2.3.1

我在 ReSTLet 2.4.2 java 中创建了一个 API,一切正常。 当我尝试在 ServerResource 文件中调用另一个 API 作为客户端时,出现此错误。

这是我作为客户端代码调用 API:

Client client = new Client(new Context(), Protocol.HTTPS);
client.getContext().getParameters().add("useForwardedForHeader","false");
ClientResource cr = new ClientResource("https://www.examples.com");
try{ 
cr.get(MediaType.APPLICATION_ALL).write(System.out); 
} catch(ResourceException | IOException e) { e.printStackTrace(); }

我遇到的错误:

The protocol used by this request is not declared in the list of client connectors. (HTTPS/1.1). In case you are using an instance of the Component class, check its "clients" property.

我尝试在 pom.xml/maven 中添加 httpclient 库,但没有成功

<dependency>
    <groupId>org.restlet.jse</groupId>
    <artifactId>org.restlet.ext.httpclient</artifactId>
    <version>2.4.0</version>
</dependency>

最佳答案

您的问题是您需要为您的 ReSTLet http 客户端配置 HTTPS 连接器。

我为您制作了这个简单的示例项目restlet https server/client sample

在此示例中,您有一个示例 HTTPS 服务器,其中包含带有自签名证书的 keystore (用于测试目的)以及配置了相同 keystore /证书的 HTTPS 客户端。

客户端代码示例:

private static final String url = "https://localhost:8443/";

public static void main(String[] args) {
    System.out.println("Sending HTTP GET request to " + url);
    // Add your HTTPS specifications
    String file = "keystore-dev.jks";
    String keystorePwd = "localhost";
    String keyPwd = "localhost";
    File keystoreFile = new File(file);

    if (keystoreFile.exists()) {
        Request request = new Request(Method.GET, url);
        System.setProperty("javax.net.ssl.trustStore", keystoreFile.getAbsolutePath());
        System.setProperty("javax.net.ssl.trustStorePassword", keystorePwd);
        System.setProperty("javax.net.ssl.keyStore", keystoreFile.getAbsolutePath());
        System.setProperty("javax.net.ssl.keyStorePassword", keystorePwd);

        Client client = new Client(new Context(), Protocol.HTTPS);
        client.getContext().getParameters().add("sslContextFactory",
                "org.restlet.engine.ssl.DefaultSslContextFactory");
        client.getContext().getParameters().add("keystoreType", "JKS");
        client.getContext().getParameters().add("keystorePath", keystoreFile.getAbsolutePath());
        client.getContext().getParameters().add("keystorePassword", keystorePwd);
        client.getContext().getParameters().add("keyPassword", keyPwd);

        Response resp = client.handle(request);

        System.out.println("Service response code: " + resp.getStatus());
        try {
            System.out.println("Service response body: " + resp.getEntity().getText());
        } catch (IOException e) {
            System.out.println("Error reading response");
        }

    } else {
        System.err.println("Error keystore not found");
    }

}

客户端 pom.xml 依赖项:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github.arielcarrera</groupId>
    <artifactId>restlet-https-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>restlet-https-client</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.restlet>2.4.0</version.restlet>
        <version.junit>4.13</version.junit>
    </properties>

    <repositories>
        <repository>
            <id>maven-restlet</id>
            <name>Restlet repository</name>
            <url>https://maven.restlet.com</url>
        </repository>
    </repositories>
    
    <dependencies>
        <dependency>
            <groupId>org.restlet.jse</groupId>
            <artifactId>org.restlet.ext.httpclient</artifactId>
            <version>${version.restlet}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${version.junit}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

创建测试 keystore 的示例命令行:

keytool -genkey -alias localhost -keyalg RSA -keysize 2048 -keypass localhost -storepass localhost -keystore keystore-dev.jks

有关更多信息,请阅读 Restlet- Client connectorsRestlet - HTTPS .

您可以使用Server/Client Resources也是。

关于java - 如何在ReSTLet的ServerResource方法中请求另一个api作为客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60299464/

相关文章:

c# - C#如何做运行时泛型?

java - 在 RESTLET 中读取 Web-INF 中的配置文件

libraries - ReSTLet 框架 - 编辑 agent.properties 文件

java - 从服务器资源调用 ReSTLet 客户端资源导致 HTTP 404 错误

java - Android Studio 启动失败 : fatal error initializing 'com.intellij.util.net.ssl.certificatemanager'

java - 学校作业多维数组麻烦

java - 如何将reSTLet过滤器迁移到spring mvc?

java - 返回 String[] 或 List<String> 的 ReSTLet 调用使用 Jackson 返回 null

java - 通过 Ant 任务运行 javac 时如何查看编译器输出?

java - 如何使用 ReSTLet 创建 Atom 表示?