https://youtu.be/cH36yhuGDxQ
EduArn is your learning hub for AI, Cloud Computing, and DevOps tools and technologies. Explore detailed guides, practical examples, and real-world use cases of artificial intelligence platforms, cloud services, automation tools, CI/CD pipelines, containers, Kubernetes, infrastructure as code, and modern IT solutions. Learn how emerging technologies are transforming software development, deployment, and business operations while building your technical skills for the future.
Showing posts with label Web Services Client. Show all posts
Showing posts with label Web Services Client. Show all posts
JAX-WS Stand Alone Web Services Using JDK1.6 Wsimport Utility Client
In this tutorial we will learn, how to create JAX-WS client by wsimport utility:
Requirements:
1. Java 1.6+
2. Eclipse
3. Windows OS or any.
4. You have already followed my first example for writing the service calss and all from click or video MathService using wsgen utility
Open the eclipse
Create the new project as java project.
File > New > Java Project.
Enter the project name: Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client
Java project will created in with src and JRE System Library
Note: assuming you have publish the earlier project Math Service and you will able to access the wisdl by any browser, URL > http://localhost:8888/webservice/www.tutorialbyexample.com?wsdl
and getting the wsdl as response.
if not then follow the previous project first, Click me
Now open the command prompt.
Change the dir: cd C:\workspace\Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client
and type the command:
C:\workspace\Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client>wsimport -s src -keep -d class http://localhost:8888/webservice/www.t
utorialbyexample.com?wsdl
Press enter, now your client supporting code will generated, like wise below screen.
Write the client code:
Right click on the src under project Stand-Alone-Web-Services-Using-JDK1.6-WsImport-Utility-JAXWS-Client.
Click New > Class.
Enter the package name:com.tutorialbyexample.mathservice.client
Enter the Class name:MathServiceClient
MathServiceClient.java code will look like:
packagecom.tutorialbyexample.mathservice.client;
importcom.tutorialbyexample.mathservice.MathService;
importcom.tutorialbyexample.mathservice.MathServiceService;
public class MathServiceClient {
public MathServiceClient() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
//
MathServiceService objMathServiceService = newMathServiceService();
MathService objMathService = objMathServiceService.getMathServicePort();
System.out.println(objMathService.add(10, 40));
}
}
Hole project directory and file will look like:
Run the MathServiceClient client.
Right click on the MathServiceClient > Run as > Java Application.
Output will appear on the console:
50.0
All done form my end, try at your end. Put your point of view if any.
Have a good day ahead!!
JAX-WS Stand Alone Web Services Using JDK1.6 Wsgen Wsimport Utility
Open the Eclipse.
File > New > Java Project
Enter the project name : Stand-Alone-Web-Services-Using-JDK6-Wsgen-Wsimport-Utility
Click on the next button
Click on the finish
Right click on the src folder under project Stand-Alone-Web-Services-Using-JDK6-Wsgen-Wsimport-Utility.
New > Class.
Enter package name: com.tutorialbyexample
Name of Class: MathService
packagecom.tutorialbyexample;
public class MathService {
public MathService() {
// TODO Auto-generated constructor stub
}
public double add(double a, double b) {
return a + b;
}
public double sub(double a, double b) {
return a - b;
}
}
Now we have to implement the annotation and have to implement it for achieving the Jax web services functionality.
After annotation its look likes:
packagecom.tutorialbyexample;
import javax.jws.WebMethod;
import javax.jws.WebParam;
importjavax.jws.WebService;
@WebService(name = "MathService", targetNamespace = "http://www.tutorialbyexample.com/MathService")
public class MathService {
public MathService() {
// TODO Auto-generated constructor stub
}
@WebMethod
public double add(@WebParam(name = "a") double a, @WebParam(name = "b") double b) {
return a + b;
}
@WebMethod
public double sub(@WebParam(name = "a") double a, @WebParam(name = "b") double b) {
return a - b;
}
}
Create the resource folder under src dir.
Open the command prompt:
Generate the JaxWS supported file by
c:\workspace\Stand-Alone-Web-Services-Using-JDK6-Wsgen-Wsimport-Utility>
wsgen -keep –classpath class -cp class -d src -r src\resource -verbose -wsdl com.tutorialbyexample.MathService
Right click on the src folder and create new class JaxWSPublish.java for publishing the JaxWS:
Enter the package name: com.tutorialbyexample.client
Enter the name: JaxWSPublish
Click finish
packagecom.tutorialbyexample.client;
importjavax.xml.ws.Endpoint;
importcom.tutorialbyexample.MathService;
public class JaxWSPublish {
public JaxWSPublish() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/webservice/www.tutorialbyexample.com", new MathService());
System.out.println("MathService has been published!");
}
}
Generated files:
Add.java:
packagecom.tutorialbyexample.jaxws;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
importjavax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "add", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "add", namespace = "http://www.tutorialbyexample.com/MathService", propOrder = {
"a",
"b"
})
public class Add {
@XmlElement(name = "a", namespace = "")
private double a;
@XmlElement(name = "b", namespace = "")
private double b;
/**
*
* @return
* returns double
*/
public double getA() {
return this.a;
}
/**
*
* @param a
* the value for the a property
*/
public void setA(double a) {
this.a = a;
}
/**
*
* @return
* returns double
*/
public double getB() {
return this.b;
}
/**
*
* @param b
* the value for the b property
*/
public void setB(double b) {
this.b = b;
}
}
AddResource.java
packagecom.tutorialbyexample.jaxws;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
importjavax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "addResponse", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addResponse", namespace = "http://www.tutorialbyexample.com/MathService")
public class AddResponse {
@XmlElement(name = "return", namespace = "")
private double _return;
/**
*
* @return
* returns double
*/
public double getReturn() {
return this._return;
}
/**
*
* @param _return
* the value for the _return property
*/
public void setReturn(double _return) {
this._return = _return;
}
}
sub.java
packagecom.tutorialbyexample.jaxws;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
importjavax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "sub", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sub", namespace = "http://www.tutorialbyexample.com/MathService", propOrder = {
"a",
"b"
})
public class Sub {
@XmlElement(name = "a", namespace = "")
private double a;
@XmlElement(name = "b", namespace = "")
private double b;
/**
*
* @return
* returns double
*/
public double getA() {
return this.a;
}
/**
*
* @param a
* the value for the a property
*/
public void setA(double a) {
this.a = a;
}
/**
*
* @return
* returns double
*/
public double getB() {
return this.b;
}
/**
*
* @param b
* the value for the b property
*/
public void setB(double b) {
this.b = b;
}
}
SubResource.java
packagecom.tutorialbyexample.jaxws;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlRootElement;
importjavax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "subResponse", namespace = "http://www.tutorialbyexample.com/MathService")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "subResponse", namespace = "http://www.tutorialbyexample.com/MathService")
public class SubResponse {
@XmlElement(name = "return", namespace = "")
private double _return;
/**
*
* @return
* returns double
*/
public double getReturn() {
return this._return;
}
/**
*
* @param _return
* the value for the _return property
*/
public void setReturn(double _return) {
this._return = _return;
}
}
MathServiceService_schema1.xsd
<?xml version="1.0"encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"targetNamespace="http://www.tutorialbyexample.com/MathService"xmlns:tns="http://www.tutorialbyexample.com/MathService"xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="add"type="tns:add"/>
<xs:element name="addResponse"type="tns:addResponse"/>
<xs:element name="sub"type="tns:sub"/>
<xs:element name="subResponse"type="tns:subResponse"/>
<xs:complexType name="add">
<xs:sequence>
<xs:element name="a"type="xs:double"/>
<xs:element name="b"type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="return"type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sub">
<xs:sequence>
<xs:element name="a"type="xs:double"/>
<xs:element name="b"type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="subResponse">
<xs:sequence>
<xs:element name="return"type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
MathServiceService.wsdl
<?xml version="1.0"encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0"targetNamespace="http://www.tutorialbyexample.com/MathService"xmlns:tns="http://www.tutorialbyexample.com/MathService"xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="add"type="tns:add"/>
<xs:element name="addResponse"type="tns:addResponse"/>
<xs:element name="sub"type="tns:sub"/>
<xs:element name="subResponse"type="tns:subResponse"/>
<xs:complexType name="add">
<xs:sequence>
<xs:element name="a"type="xs:double"/>
<xs:element name="b"type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence>
<xs:element name="return"type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sub">
<xs:sequence>
<xs:element name="a"type="xs:double"/>
<xs:element name="b"type="xs:double"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="subResponse">
<xs:sequence>
<xs:element name="return"type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Run the Publish class:
Double click on the JaxWSPublish
or
Right click on the JaxWSPublish > Run > Java Application
In console you will get:
MathService has been published!
Now open the browser and type:
http://localhost:8888/webservice/www.tutorialbyexample.com
WSDL:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy"xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://www.tutorialbyexample.com/MathService" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"targetNamespace="http://www.tutorialbyexample.com/MathService" name="MathServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://www.tutorialbyexample.com/MathService" schemaLocation="http://localhost:8888/webservice/www.tutorialbyexample.com?xsd=1"/>
</xsd:schema>
</types>
<message name="add">
<part name="parameters" element="tns:add"/>
</message>
<message name="addResponse">
<part name="parameters" element="tns:addResponse"/>
</message>
<message name="sub">
<part name="parameters" element="tns:sub"/>
</message>
<message name="subResponse">
<part name="parameters" element="tns:subResponse"/>
</message>
<portType name="MathService">
<operation name="add">
<input wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/addRequest" message="tns:add"/>
<output wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/addResponse" message="tns:addResponse"/>
</operation>
<operation name="sub">
<input wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/subRequest" message="tns:sub"/>
<output wsam:Action="http://www.tutorialbyexample.com/MathService/MathService/subResponse" message="tns:subResponse"/>
</operation>
</portType>
<binding name="MathServicePortBinding" type="tns:MathService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="add">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="sub">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="MathServiceService">
<port name="MathServicePort" binding="tns:MathServicePortBinding">
<soap:address location="http://localhost:8888/webservice/www.tutorialbyexample.com"/>
</port>
</service>
Done from my end, try at your end and put your point of view if any.
Have good day ahead!!!
Subscribe to:
Posts (Atom)





