Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey client with RxJava conflicts with resteasy

I have a web application deployed to JBOSS. It contains dependency to jersey-rx-client-rxjava package and one of the packages has transient dependency to resteasy-jaxrs.

I have the following code.

RxObservable.newClient()
        .target(fullURL)
        .request()
        .header("Authorization", "Bearer " + config.getApiKey())
        .rx()
        .post(javax.ws.rs.client.Entity.entity(context, MediaType.APPLICATION_JSON_TYPE), AIResponse.class)
        .map(new Func1<AIResponse, String>() {
            @Override
            public String call(AIResponse res) {
                return res.getType();
            }
        })
        .subscribe(new Action1<String>() {
            @Override
            public void call(final String type) {
                Log.info(type);
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(final Throwable throwable) {
                //async.resume(throwable);
                Log.error(throwable.getMessage(), throwable);
            }
        }, new Action0() {
            @Override
            public void call() {
                //async.resume(throwable);
                 Log.info("Done");
            }
        });

At this line, the following exception is thrown.

final JerseyInvocation invocation = (JerseyInvocation)   getBuilder().build(name, entity);

Why does the build method return org.jboss.resteasy.client.jaxrs.internal.ClientInvocation, instead of JerseyInvocation?

2017-03-20 12:18:43,678 ERROR [com.optawork.bot.CustomResource] (default task-2) org.jboss.resteasy.client.jaxrs.internal.ClientInvocation cannot be cast to org.glassfish.jersey.client.JerseyInvocation: java.lang.ClassCastException: org.jboss.resteasy.client.jaxrs.internal.ClientInvocation cannot be cast to org.glassfish.jersey.client.JerseyInvocation
        at org.glassfish.jersey.client.rx.rxjava.JerseyRxObservableInvoker$2.call(JerseyRxObservableInvoker.java:89)
        at org.glassfish.jersey.client.rx.rxjava.JerseyRxObservableInvoker$2.call(JerseyRxObservableInvoker.java:83)
        at rx.Observable.unsafeSubscribe(Observable.java:10142)
        at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
        at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
        at rx.Observable.subscribe(Observable.java:10238)
        at rx.Observable.subscribe(Observable.java:10205)
        at rx.Observable.subscribe(Observable.java:10086)
        at ai.api.AIDataService.converse(AIDataService.java:601)
like image 485
Wong Liong Hung Avatar asked May 02 '26 17:05

Wong Liong Hung


1 Answers

Why does the build method return org.jboss.resteasy.client.jaxrs.internal.ClientInvocation, instead of JerseyInvocation

It's just how the JAX-RS Client API is designed. When we try to call ClientBuilder.newBuilder (which is done internally), the JAX-RS API does a service lookup for any implementation of the JAX-RS Client API. If there is none, it falls back to Jersey. The problem is that when the service lookup is done, RESTEasy's client is found on the classpath.

The Jersey RX API has a from(Client) method that we can user, instead of the default newClient. This will allow us to pass an explicit JerseyClient instead of using the JAX-RS API ClientBuilder.newBuilder/newClient

// actual JerseyClient which implements Client
Client client = new JerseyClientBuilder().build();
RxObservable.from(client)

JerseyClientBuilder has pretty much the same API as the JAX-RS ClientBuilder, so you can use it pretty much the same way.

like image 181
Paul Samsotha Avatar answered May 06 '26 14:05

Paul Samsotha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!