In MyEclipse , it is very easy to build web service base on some web service framework.
Let’s build a JAX-WS web service.
1.Create a web service project in MyEclipse, which name is TestWS_JAXWS.
chose JAX-WS framework
It automatically produces J2EE directory.
2. import JAX-WS 2.1 library into the project
chose MyEclipse Libraries
Select JAX-WS 2.1 Libraries
3. Build a simple java which includes several methods
public class Calculator { public int add(int a, int b) { return (a + b); } public int subtract(int a, int b) { return (a - b); } public int multiply(int a, int b) { return (a * b); } public int divide(int a, int b) { return (a / b); } }
4. Build a new web service
chose JAX-WS Framework and ‘Create web service from Java class’
In Java Class field, input ‘com.jack.ws.Calculator’ which is created at above step
It automatically fill in other fields,
Modify target namespace field to ‘http://localhost:8080/TestWS_JAXWS’
tick ‘Generate WSDL in project’
5. After click Finish button, it produces a lot of files, directory and class
CalculatorDelegate class
wsdl directory, web.xml under WEB-INF
CalculatorDelegate.java
@javax.jws.WebService(targetNamespace = "http://localhost:8080/TestWS_JAXWS", serviceName = "CalculatorService", portName = "CalculatorPort", wsdlLocation = "WEB-INF/wsdl/CalculatorService.wsdl") public class CalculatorDelegate { com.jack.ws.Calculator calculator = new com.jack.ws.Calculator(); public int add(int a, int b) { return calculator.add(a, b); } public int subtract(int a, int b) { return calculator.subtract(a, b); } public int multiply(int a, int b) { return calculator.multiply(a, b); } public int divide(int a, int b) { return calculator.divide(a, b); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>JAX-WS endpoint - CalculatorService</description> <display-name>CalculatorService</display-name> <servlet-name>CalculatorService</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CalculatorService</servlet-name> <url-pattern>/CalculatorPort</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener></web-app>
sun-jaxws.xml
<?xml version = "1.0"?> <endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"> <endpoint name="CalculatorPort" implementation="com.jack.ws.CalculatorDelegate" url-pattern="/CalculatorPort"> </endpoint></endpoints>
CalculatorService.wsdl
<?xml version="1.0" encoding="UTF-8"?> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. --> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8080/TestWS_JAXWS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CalculatorService" targetNamespace="http://localhost:8080/TestWS_JAXWS"> <types> <xsd:schema> <xsd:import namespace="http://localhost:8080/TestWS_JAXWS" schemaLocation="CalculatorService_schema1.xsd"/> </xsd:schema> </types> <message name="add"> <part element="tns:add" name="parameters"/> </message> <message name="addResponse"> <part element="tns:addResponse" name="parameters"/> </message> <message name="divide"> <part element="tns:divide" name="parameters"/> </message> <message name="divideResponse"> <part element="tns:divideResponse" name="parameters"/> </message> <message name="multiply"> <part element="tns:multiply" name="parameters"/> </message> <message name="multiplyResponse"> <part element="tns:multiplyResponse" name="parameters"/> </message> <message name="subtract"> <part element="tns:subtract" name="parameters"/> </message> <message name="subtractResponse"> <part element="tns:subtractResponse" name="parameters"/> </message> <portType name="CalculatorDelegate"> <operation name="add"> <input message="tns:add"/> <output message="tns:addResponse"/> </operation> <operation name="divide"> <input message="tns:divide"/> <output message="tns:divideResponse"/> </operation> <operation name="multiply"> <input message="tns:multiply"/> <output message="tns:multiplyResponse"/> </operation> <operation name="subtract"> <input message="tns:subtract"/> <output message="tns:subtractResponse"/> </operation> </portType> <binding name="CalculatorPortBinding" type="tns:CalculatorDelegate"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="add"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="divide"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="multiply"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="subtract"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="CalculatorService"> <port binding="tns:CalculatorPortBinding" name="CalculatorPort"> <soap:address location="http://localhost:8080/TestWS_JAXWS/CalculatorPort"/> </port> </service> </definitions>
6.Launch SOAP Web Service Explore to make a test to the web sercie
There are two Explores to be chosen. Select one.
Then click ‘WSDL Page’ in right-top , then click ‘WSDL Main’ in left-top
Input “http://localhost:8080/TestWS_JAXWS/CalculatorPort?WSDL”
Notice:
1) CalculatorPort match the servlet-mapping in web.xml
2)?WSDL
This is a universal query string argument that can be added to the end of any web service which will tell the web service to return it’s full WSDL to the caller. In this case, the WSDL is returned to our Web Services Explorer tool which loads it up, and displays the web services exposed operations to us.
click ‘add’, ‘divide’,’subtract’,’multiply’ etc methods
e.g. add, input 100, 555, add go, then will get a return value:655
e.g. subtract, input 1000, 20 , will get 980
7. Build a new Java project as the client to use the web service, which name is TestWS_JAXWSClient
8. Build a new web service client in the project
Chose JAX-WS framework
In WSDL URL input ‘http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl’
Create a java package
It has a WSDL validation, click finish button
9. It automatically produces a lot of classes under new created java package
It is very easy to understand the meaning of those classes from literal
10. Build a Test Class in the java client project
public class Test {
public static void main(String[] args) { CalculatorService cs = new CalculatorService(); CalculatorDelegate cd = cs.getCalculatorPort();
System.out.println("3 + 10 = "+cd.add(3,10)); System.out.println("999 - 222 = "+cd.subtract(999,222)); System.out.println("250 / 5 = "+cd.divide(250, 5)); System.out.println("7 * 23 = "+cd.multiply(7,23)); } }
11. Run Test.java, it will get
3 + 10 = 13 999 - 222 = 777 250 / 5 = 50 7 * 23 = 161
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
/**
* This class was generated by the JAX-WS RI. JAX-WS RI 2.1.3-hudson-390-
* Generated source version: 2.0
* <p>
* An example of how this class may be used:
*
* <pre>
* CalculatorService service = new CalculatorService();
* CalculatorDelegate portType = service.getCalculatorPort();
* portType.add(…);
* </pre>
*
* </p>
*
*/
@WebServiceClient(name = “CalculatorService”, targetNamespace = “http://localhost:8080/TestWS_JAXWS”, wsdlLocation = “http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl”)
public class CalculatorService extends Service {
private final static URL CALCULATORSERVICE_WSDL_LOCATION;
private final static Logger logger = Logger
.getLogger(com.jack.ws.CalculatorService.class.getName());
static {
URL url = null;
try {
URL baseUrl;
baseUrl = com.jack.ws.CalculatorService.class.getResource(“.”);
url = new URL(baseUrl,
“http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl”);
} catch (MalformedURLException e) {
logger
.warning(“Failed to create URL for the wsdl Location: ‘http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl’, retrying as a local file”);
logger.warning(e.getMessage());
}
CALCULATORSERVICE_WSDL_LOCATION = url;
}
public CalculatorService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CalculatorService() {
super(CALCULATORSERVICE_WSDL_LOCATION, new QName(
“http://localhost:8080/TestWS_JAXWS”, “CalculatorService”));
}
/**
*
* @return returns CalculatorDelegate
*/
@WebEndpoint(name = “CalculatorPort”)
public CalculatorDelegate getCalculatorPort() {
return super.getPort(new QName(“http://localhost:8080/TestWS_JAXWS”,
“CalculatorPort”), CalculatorDelegate.class);
}
}
credit repair rhode island…
Hey I like your site so much that I just bookmarked it….
Trackback by credit repair rhode island — August 2, 2010 @ 5:36 am