Generate Web Service Client with JAX-WS Maven Plugin:
There are many ways and techniques to create a web service client in java, however here, in my this blog I am creating a web service client project with JAX-WS using maven.
JAX-WS allows us to invoke a web service, as if we were making a local method call. For this to happen a standard mapping from WSDL to Java has been defined.
The Service Endpoint Interface (SEI) is the Java representation of the web service endpoint.
At runtime JAX-WS creates an instance of a SEI that can be used to make web service calls by simply making method calls on the SEI.
The stubs have been generated by the WSDL. So the stubs are a prerequisite.
Now to begin with, create a simple project using maven, this project would be your web service client.
1) Maven Configuration:
Into your pom.xml file add the following plugin:
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.sample.stub</packageName>
<wsdlDirectory>WebServiceDescription</wsdlDirectory>
<sourceDestDir>src/main/java</sourceDestDir>
</configuration>
</plugin>
Create a directory named WebServiceDescription, as specified in the above plugin (between the tag <wsdlDirectory>) and place your wsdl file into this directory.
2) Generating stub classes:
For generating stub use the following maven command
mvn clean jaxws:wsimport
The above command jaxws:wsimport generates JAX-WS portable artifacts used in JAX-WS clients and services. The tool reads a WSDL and generates all the required artifacts for web service development, deployment, and invocation.
Check the com.sample.stub package it should contain all the required stub classes. Which comprise of
Service class
Object-Factory class
service-method class
returntype class
package-info class etc.
3) Access the Web service:
Here is a custom Java class code used to access
System.out.println("Customer Name: "+response.getCustomerName());
catch (Exception e) {
e.printStackTrace();
}
Done, any comments are appreciated.
There are many ways and techniques to create a web service client in java, however here, in my this blog I am creating a web service client project with JAX-WS using maven.
JAX-WS allows us to invoke a web service, as if we were making a local method call. For this to happen a standard mapping from WSDL to Java has been defined.
The Service Endpoint Interface (SEI) is the Java representation of the web service endpoint.
At runtime JAX-WS creates an instance of a SEI that can be used to make web service calls by simply making method calls on the SEI.
The stubs have been generated by the WSDL. So the stubs are a prerequisite.
Now to begin with, create a simple project using maven, this project would be your web service client.
1) Maven Configuration:
Into your pom.xml file add the following plugin:
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.sample.stub</packageName>
<wsdlDirectory>WebServiceDescription</wsdlDirectory>
<sourceDestDir>src/main/java</sourceDestDir>
</configuration>
</plugin>
Create a directory named WebServiceDescription, as specified in the above plugin (between the tag <wsdlDirectory>) and place your wsdl file into this directory.
2) Generating stub classes:
For generating stub use the following maven command
mvn clean jaxws:wsimport
The above command jaxws:wsimport generates JAX-WS portable artifacts used in JAX-WS clients and services. The tool reads a WSDL and generates all the required artifacts for web service development, deployment, and invocation.
Check the com.sample.stub package it should contain all the required stub classes. Which comprise of
Service class
Object-Factory class
service-method class
returntype class
package-info class etc.
3) Access the Web service:
Here is a custom Java class code used to access
package com.sample.client;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import com.sample.stub.CustomerInfoBean;
import com.sample.stub.CustomerInfoBeanService;
import com.sample.stub.CustomerType;
public class CustomerInfoClient {
public static void main(String[] args) {
try {
QName qName = new QName("http://sample.com/","CustomerInfoBeanService");
URL url = new URL("http://localhost:7001/CustomerInfoBean/CustomerInfoBeanService?WSDL");
CustomerInfoBeanService wsClient = new CustomerInfoBeanService(url,qName);
CustomerInfoBean wsPort = wsClient.getCustomerInfoBeanPort();
CustomerType response = wsPort.getCustomerInfo(1);
System.out.println("Customer Id: "+response.getCustomerId());System.out.println("Customer Name: "+response.getCustomerName());
} catch (MalformedURLException e) {
e.printStackTrace();
}
e.printStackTrace();
}
}
}
Above I am accessing a web service which I had created in my previous blog.
- Now, let me explain some of the above code snippet. Beginning with QName, A
QName
object is an object that represents an XML qualified name. The object is composed of a namespace URI and the local part of the qualified name i.e. the service name CustomerInfoBeanService. - In the next line, the URL object file points to the WSDL that was deployed:
- http://localhost:7001/CustomerInfoBean/CustomerInfoBeanService?WSDL
- With the next line of code we are able to get through the service. The wsClient is the service object.
- Further we create a port, this can be done by invoking the get port method on service object. The method name usually begin is getPortTypePort() where PortType is as specified in wsdl.
- At this point we have got the service port. We can now invoke our method on this port.
Output:
Customer Id: 1
Customer Name: abc
Done, any comments are appreciated.
Thanks a lot for this tip about jax-ws client
ReplyDeleteThank you.
Deletethanks a lot for your publication ....
ReplyDeleteSimply Great!! Finally I've got my Maven + JAX-WS Client!
ReplyDeleteThanks a lot
Can you send sample application
ReplyDeleteareef.on@gmail.com
Regards
Areef
Thanks! This post makes me win hours of work.
ReplyDeleteMy first Soap call from scratch!
When i try to test this example on the server doesn't know the classes of the webservice.. Any Suggestion?
ReplyDeleteI got the following issue:
ReplyDelete"trace": "java.lang.NoClassDefFoundError: javax/xml/ws/Service\r\n\tat java.lang.ClassLoader.defineClass1(Native Method)\r\n\tat java.lang.ClassLoader.defineClass(ClassLoader.java:763)\r\n\tat java.lang.ClassLoader.defineClass(ClassLoader.java:642)\r\n\tat io.apiman.common.plugin.PluginClassLoader.findClass(PluginClassLoader.java:185)\r\n\tat java.lang.ClassLoader.loadClass(ClassLoader.java:424)\r\n\tat java.lang.ClassLoader.loadClass(ClassLoader.java:357)\r\n\tat io.apiman.plugins.config_policy_http_webservice.ConnectionServiceSoap.testWebserviceRCP(ConnectionServiceSoap.java:122)\r\n\tat io.apiman.plugins.config_policy_http_webservice.ConfigPolicyHttpWebservice.doApply(ConfigPolicyHttpWebservice.java:79)\r\n\tat io.apiman.plugins.config_policy_http_webservice.ConfigPolicyHttpWebservice.doApply(ConfigPolicyHttpWebservice.java:39)\r\n\tat io.apiman.gateway.engine.policies.AbstractMappedPolicy.apply(AbstractMappedPolicy.java:70)\r\n\tat io.apiman.gateway.engine.policy.RequestChain.applyPolicy(RequestChain.java:68)\r\n\tat io.apiman.gateway.engine.policy.Chain.doApply(Chain.java:148)\r\n\tat
This seems to be an issue with java class loader. I had created this project in Java 6, which version of Java are you working on.
DeleteSorry about delayed response. This was done with Java 6.
ReplyDelete