Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simply consuming a web service in Java

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.

like image 273
Deano Avatar asked Nov 15 '08 00:11

Deano


People also ask

What is web service in Java with example?

Web service is a technology to communicate one programming language with another. For example, java programming language can interact with PHP and . Net by using web services. In other words, web service provides a way to achieve interoperability.


Video Answer


4 Answers

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();
like image 165
FoxyBOA Avatar answered Oct 03 '22 12:10

FoxyBOA


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:

  • JAX-WS 2.1 and JAXB 2.1 is available in JDK 6 Update 4 release

  • Getting Started with JAX-WS Web Services (tutorial to use the JDK built-in JAX-WS for deploying and consuming a web service)

like image 45
anjanb Avatar answered Oct 03 '22 11:10

anjanb


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.

like image 41
Dónal Avatar answered Oct 03 '22 12:10

Dónal


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.

like image 37
Chris Vest Avatar answered Oct 03 '22 10:10

Chris Vest