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.
No comments:
Post a Comment