JAX-RX client embedded into Module leads to Exception

Custom indicators, trading strategies, data export and recording and more...
hesaid
Posts: 21
Joined: Tue Sep 01, 2020 12:34 pm
Has thanked: 1 time
Been thanked: 1 time

JAX-RX client embedded into Module leads to Exception

Post by hesaid » Tue Sep 29, 2020 5:25 pm

Hi,
I have a very simple task, I need to make a call to the RESTFul Web Service from the Module. I use JAX-RS/Jersey library to create a JAX-RS client.
I have designed a testing class with @Layer1SimpleAttachable annotation. It creates a panel with the button inside. Pushing the button sends request to the public url https://gorest.co.in, just to test the prototype.The request causes java.lang.ExceptionInInitializerError. The sample code is pretty simple and works as a standalone application so I can only guess that there are some problems during dynamic class loading. In fact I'm ready to change the library to some other. Eitherway I'd like to get your recommendations either on how to change the code or which library to choose. Thanks.
Here is the code, you can change the url with your own, it doesn't matter.


import velox.api.layer1.annotations.Layer1ApiVersion;
import velox.api.layer1.annotations.Layer1ApiVersionValue;
import velox.api.layer1.annotations.Layer1SimpleAttachable;
import velox.api.layer1.annotations.Layer1StrategyName;
import velox.api.layer1.data.InstrumentInfo;
import velox.api.layer1.simplified.Api;
import velox.api.layer1.simplified.CustomModule;
import velox.api.layer1.simplified.CustomSettingsPanelProvider;
import velox.api.layer1.simplified.InitialState;
import velox.gui.StrategyPanel;

import javax.swing.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import java.awt.*;
import java.io.IOException;

@Layer1SimpleAttachable
@Layer1StrategyName("Initializer")
@Layer1ApiVersion(Layer1ApiVersionValue.VERSION2)
public class Initializer implements CustomModule, CustomSettingsPanelProvider {

private void testJaxRS() throws InterruptedException, IOException, NoSuchMethodException {
Client client = ClientBuilder.newClient();
String result = client.target("https://gorest.co.in/public-api/posts")
.request(MediaType.APPLICATION_JSON)
.get(String.class);
}


@Override
public void initialize(String alias, InstrumentInfo instrumentInfo, Api api, InitialState initialState) {

}

@Override
public void stop() {
}

/**
* Custom panel for initializer
* @return
*/
@Override
public StrategyPanel[] getCustomSettingsPanels() {
StrategyPanel p1 = new StrategyPanel("JAX-RS Client test");
JButton connectButton = new JButton("Call");
connectButton.addActionListener(e -> {
try {
testJaxRS();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (NoSuchMethodException noSuchMethodException) {
noSuchMethodException.printStackTrace();
}
});
p1.setLayout(new BorderLayout());
p1.add(connectButton, BorderLayout.CENTER);
return new StrategyPanel[]{p1};
}
}
Last edited by hesaid on Mon Nov 09, 2020 11:13 am, edited 2 times in total.

Andry API support
Posts: 548
Joined: Mon Jul 09, 2018 11:18 am
Has thanked: 25 times
Been thanked: 85 times

Re: JAX-RX client embedded into Module leads to Exception

Post by Andry API support » Wed Sep 30, 2020 1:33 pm

Hi hesaid,
...to change the library to some other
I tried resteasy-client(compile group: 'org.jboss.resteasy', name: 'resteasy-client', version: '4.5.8.Final') and it is working. ApacheHttpClient 4.5.2 is used in Bookmap but it may require some more lines of code.

Code: Select all

        ResteasyClient client = new ResteasyClientBuilderImpl().build();
        ResteasyWebTarget target = client.target("https://gorest.co.in/public-api/posts");
        Response response = target.request().get();
        String value = response.readEntity(String.class);
        Log.info(value);

hesaid
Posts: 21
Joined: Tue Sep 01, 2020 12:34 pm
Has thanked: 1 time
Been thanked: 1 time

Re: JAX-RX client embedded into Module leads to Exception

Post by hesaid » Wed Oct 07, 2020 4:42 pm

Thanks Andrey!

Post Reply