Simple example of consuming wsdl webservice with Java?
Asked Answered
M

3

8

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?

Marentic answered 21/8, 2011 at 19:48 Comment(1)
I am also looking in to same issue..did you got solution for this?Habitat
D
10

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

[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+.

Divers answered 21/8, 2011 at 19:52 Comment(4)
Unless I'm misreading it, that page is about creating a new webservice in Java? That is not what I need. I am asking what the Java code to consume an existing webservice is.Marentic
Ah, my mistake, I thought your were looking to implement a webservice from WSDL, not consume it.Divers
I guess the "createWebservice" in the question could have been confusing - I've renamed it to "setupWebserviceObject" instead.Marentic
WSDL consumption is simple. Using other tools, for other programming languages.Allot
M
1

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.

Metheglin answered 3/2, 2014 at 14:45 Comment(0)
W
0

I would recommend using axis2 command line tools, most simply:

java2wsdl -cn fully-qualified-class-name

wsdl2java -uri wsdlLocation -ss -sd -uw -g -o outputLocation

cd into outputLocation and run ant

Put generated .aar file to WEB-INF/services folder to create service (dont need if you just want client) and copy generated stub files to your source folder. You can use YourServiceSkeleton class to implement business logic and use YourServiceStub class for the client

Wera answered 21/8, 2011 at 19:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.