36

I have a very simple SOAP web service that I need to consume from a Java client. What is the easiest way to accomplish this without using any third party libraries? A requirement is that the host and port is read from the web.xml before every call to the ws.

0

4 Answers 4

22

Depending on which version of JAVA you're using, some of the JAX-WS is built into it. JDK 6 has Java's JAX-WS standard implementation and you could just use it.

See the following:

1
  • JAX-WS does look very easy to use, but how can it be used to read the host name from the web.config file?
    – Deano
    Commented Nov 17, 2008 at 23:39
22

I can recommend you CXF library. Using it you will have several options for calling web services:

  1. Use dynamic proxy for calling (don't need to make Java stubs using wsdl2java).

    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://admin:password@localhost:8080"+
                                     "/services/MyService?wsdl");
    Object[] a = client.invoke("test", "");
    System.out.println(a);
    
  2. Using Java stub generated from WSDL, using wsdl2java.

  3. If your server was created using CXF you can reuse your interface code directly (instead of using wsdl2java on the WSDL which was created from your interface!)

For both #2 and #3, the following code exemplifies the CXF usage:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://admin:password@localhost:8080/services/MyService");
factory.setServiceClass(ITest.class);
ITest client = (ITest) factory.create();
client.test();
3
  • Does the DynamicClientFactory require you to create the stub first?
    – vikasde
    Commented Feb 22, 2010 at 14:40
  • 2
    No, you can dynamically execute methods from wsdl file, pass parameters, got result (scalar or xml object).
    – FoxyBOA
    Commented Feb 23, 2010 at 12:14
  • The question stated explicitly "without using any third party libraries". Commented Mar 14, 2018 at 11:15
8

If you can relax your "no 3rd party libraries" requirement, and you have a WSDL for the web service then Axis makes it really easy. Just compile the WSDL using wsdl2java, and you can use the generated Java classes to consume the web service.

1

Without using any third party libraries? Get to know the SOAP standard really well and learn to love SAX.

If you can't love SAX, then lax your no-third-party-libs requirement and use StAX (with woodstox) instead.

This approach might be the "easiest" (considering the no-third-party-libs requirement) but I don't think it will be easy.

2
  • I don't understand why it would be so difficult? Surely all you have to do is construct the request using a stringbuffer, open a connection then stuff the request down the connection and wait for a response. Or am I missing something here?
    – Deano
    Commented Nov 15, 2008 at 0:52
  • 5
    What you may be missing is the difficulty/tedium of constructing the request using StringBuffer. Its certainly possibly, but I wouldnt recommend it. The same goes for reading the response without a SOAP library.
    – Dónal
    Commented Nov 15, 2008 at 1:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.