Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example of consuming wsdl webservice with Java?

I'm trying to consume a WSDL webservice in Java, in what will eventually be an Eclipse plugin.

I can use File>New>Other to select "Web Service Client" which works, but it generates a bunch of files that would have to changed/regenerated when the webservice changes, which is rather rubbish.

Everywhere I look I'm seeing assorted ways of doing things, but I can't get any of them to actually do what I want.

Here's some code:

String WsdlUrl = "http://localhost:port/path/to/wsdl";

ArrayList<String> args = new ArrayList();
args.add("arg1");
args.add("arg2");
// etc

Webservice ws = setupWebserviceObject( WsdlUrl );
Object result = ws.invoke("methodname",args);

System.out.println(result);

Basically what I need is to change "Webservice", "setupWebserviceObject", and "invoke" into whatever works, without needing pre-generated classes and with a minimum of any other annoying fluff.

It doesn't seem like it should be difficult to do, but so far I've not found a clear example of how to do it.

Can anyone help?

like image 971
Peter Boughton Avatar asked Aug 21 '11 19:08

Peter Boughton


People also ask

How do I consume a WSDL?

To consume a SOAP Web Service in your application, do the following: In the Logic tab, open the Integrations folder. Right-click the SOAP element and select Consume SOAP Web Service.... In the displayed dialog, specify the location of the Web Service definition (WSDL) and click Consume.

How do you consume a web service explain with examples?

A web service is any piece of software that makes itself available over the internet and uses a standardized XML messaging system. XML is used to encode all communications to a web service. For example, a client invokes a web service by sending an XML message, then waits for a corresponding XML response.


2 Answers

I suppose the concept of "simple" is incompatible with all things WSDL but here are some examples:

  • Creating a dynamic web service client from WSDL using JAX-WS.

  • Creating a web service client using the Apache Axis2 Axiom API.

[Note] I've kept the original, misunderstood response below, in case it helps anyone.

This article is a good summary of your options for implementing a service from WSDL: 5 Techniques for Creating Java Web Services from WSDL.

The JAX-WS Provider API implementation might be the easiest route if you are using Java 6+.

like image 183
maerics Avatar answered Nov 10 '22 00:11

maerics


Simple way step by step:

This was made using Apache CXF and Maven dependency management.

1 - Get the WSDL descriptor of the service saved in a file. Put it in the resources folder of your project (folder should be in the Source folders list of your project, if you are using eclipse).

2 - In the pom.xml declare the dependencies:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>2.7.7</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>2.7.7</version>
</dependency>

3 - Use following Maven plugin to generate the java classes:

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>2.7.7</version>
        <executions>
            <execution>
                <id>generate-sources</id>
                <phase>generate-sources</phase>
                <configuration>
                    <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                    <wsdlOptions>
                        <wsdlOption>
                            <wsdl>${basedir}/resources/WebService.wsdl.xml</wsdl>
                        </wsdlOption>
                    </wsdlOptions>
                </configuration>
                <goals>
                    <goal>wsdl2java</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

4 - Use following code to make the call:

String methodName = "getSomethingFromMyWebService";
DynamicClientFactory dcf = DynamicClientFactory.newInstance();
Client client = dcf.createClient(ConsumeTest.class.getClassLoader().getResource("WebService.wsdl.xml"));

Object[] res = client.invoke(methodName,parameter1,parameter2, parameterN);
SomethingObject[] somethingObjectList = (SomethingObject[])res[0];
Class.forName(res.getClass().getName()).isArray();
for(SomethingObject so : somethingObjectList){
    // do something!
}

5 - Profit!

Notes: If the method does not return a list of something you have to cast to the object it returns instead.

like image 38
Ric Jafe Avatar answered Nov 10 '22 00:11

Ric Jafe