Apache CXF + JavaFX No conduit initiator was found for the namespace

4.7k views Asked by At

I'm triying to run a JavaFX Rest client using CXF. A very simple test. When I try to get an URL I get the org.apache.cxf.BusException: No conduit initiator was found for the namespace http://cxf.apache.org/transports/http. I took a look at some related questions here, but no luck. Any help would be appreciated. Then only maven dependency I added was cxf-rt-rs-client 3.1.0 The code is:

WebClient client = WebClient.create("http://www.stackoverflow.com"); client.type("text/html").accept("text/html"); System.out.println(client.get());

Stacktrace:

Caused by: org.apache.cxf.BusException: No conduit initiator was found for the namespace http://cxf.apache.org/transports/http.
at org.apache.cxf.bus.managers.ConduitInitiatorManagerImpl.getConduitInitiator(ConduitInitiatorManagerImpl.java:110)
at org.apache.cxf.endpoint.AbstractConduitSelector.getSelectedConduit(AbstractConduitSelector.java:104)
at org.apache.cxf.endpoint.UpfrontConduitSelector.selectConduit(UpfrontConduitSelector.java:77)
at org.apache.cxf.message.ExchangeImpl.getConduit(ExchangeImpl.java:159)
at org.apache.cxf.interceptor.MessageSenderInterceptor.getConduit(MessageSenderInterceptor.java:71)
at org.apache.cxf.interceptor.MessageSenderInterceptor.handleMessage(MessageSenderInterceptor.java:46)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:624)
at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1100)
3

There are 3 answers

3
David J. Liszewski On

You are fine with your Maven dependencies.

Client construction looks a bit off per CXF 3.x guides, wherein JAX-RS 2.0 is supported.

See AX-RS 2.0 Client API.

Try this code:

    WebTarget target = ClientBuilder.newClient().target("http://stackoverflow.com/");

    Response response = target.request().get();
    System.out.println(response.getEntity().getClass().getName());

Using this code, you will learn the response entity is an input stream .. a sequence of characters being the HTML content of the StackOverflow home page.

If you're feeling adventurous, and to demonstrate I'm not a charlatan, add the following dependency to your POM:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency> 

and then attempt this:

    WebTarget target = ClientBuilder.newClient().target("http://stackoverflow.com/");
    System.out.println(IOUtils.toString((InputStream) target.request().get().getEntity(), "UTF-8"));

You will be rewarded with a textual rendering (on standard output) of the StackOverflow home page – equivalent to performing a "view source" operation in your browser.

I don't know what your ultimate goal is, but if you're attempting to build anything useful from information on the StackExchange network, I suggest use of their APIs documented here.

Best of luck!

0
Hung Duc Le On

I got the same exception when using Apache CXF REST client in JavaFX project. The code is below:

MyClass rest = (MyClass) JAXRSClientFactory.create(endpoint, MyClass.class, Collections.singletonList(new JacksonJsonProvider()));
System.out.println("Service health: " + rest.health()); 

A test with plain Java project works fine with the same code and same dependencies. It is apparently a conflict between JavaFX and Apache CXF. I am trying to figure out why.

If you guys already solved this issue, that should be great to update this thread, which is the only result on Google search.

Updated solution: After a while, I found that the default Maven project does not include enough the dependencies in the plugin "maven-dependency-plugin". I tried to add more packages in the list but still not work. So the final solution is in this thread: How to package an Apache CXF application into a monolithic JAR with the Maven "shade" plugin. Shade plugin is much better and works.

1
smishra On

The shading overwrites bus-extension.txt file. Programmatically your can fix it by initializing it.

void initializeCxf() {
    final Bus defaultBus = BusFactory.getDefaultBus();
    final ConduitInitiatorManager extension = defaultBus.getExtension(ConduitInitiatorManager.class);
    extension.registerConduitInitiator("http://cxf.apache.org/transports/http", new HTTPTransportFactory());
}

Based on the comment by @hba you can also try following in case the above does not work

extension.registerConduitInitiator("http://cxf.apache.org/transports/http", new HTTPTransportFactory(defaultBus));