Home » Java » java – how to request another api as Client in ServerResource method in Restlet-Exceptionshub

java – how to request another api as Client in ServerResource method in Restlet-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment


I have created a API in Restlet 2.4.2 java everything is working fine.
I get this error when I try to call another API as a client in my ServerResource file.

here is my calling API as client code:

Client client = new Client(new Context(), Protocol.HTTPS);
ClientResource cr = new ClientResource("https://www.examples.com");
} catch(ResourceException | IOException e) { e.printStackTrace(); }

Error I’m facing:

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.

I Tried to add httpclient library in pom.xml/maven but no luck

How to&Answers:

Your problem is that you need to configure an HTTPS Connector to your restlet http client.

I made this simple sample project for you restlet https server/client sample

In this sample, you have a sample HTTPS server with a keystore with a self-signed certificate (for testing purpose) and an HTTPS client with same keystore/certificate configured.

Client code sample:

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("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");


Client pom.xml dependencies:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">




            <name>Restlet repository</name>


Sample command line to create a test keystore:

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

For more information, read about Restlet- Client connectors and Restlet – HTTPS.

You can use Server/Client Resources too.

Hope it helps.