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 c开发者_JAVA技巧an 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?
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+.
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.
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
精彩评论