package fromjava.server;
public class AddNumbersException extends Exception {
private static final long serialVersionUID = 1L;
String detail;
public AddNumbersException (String message, String detail) {
super (message);
this.detail = detail;
}
public String getDetail () {
return detail;
}
}
|
package fromjava.server;
import javax.xml.ws.Endpoint;
public class AddWebservice {
public static void main (String[] args) throws Exception {
Endpoint.publish (
"http://localhost:8080/jaxws20test/addnumbers",
new AddNumbersImpl ());
}
}
|
package fromjava.server;
import javax.jws.WebService;
@WebService
public class AddNumbersImpl { public int addNumbers(int number1, int number2) throws AddNumbersException { if (number1 < 0 || number2 < 0) { throw new AddNumbersException("Negative number cant be added!", "Numbers: " + number1 + ", " + number2); } return number1 + number2; } }
|
set path=%path%;E:CommonLibjwsdp-2.0jaxwsbin;
set classpath=%classpath%;E:CommonLibjwsdp-2.0jaxwslibjaxws-api.jar;E:CommonLibjwsdp-2.0jaxwslibjaxws-rt.jar;E:CommonLibjwsdp-2.0jaxwslibjaxws-tools.jar;E:CommonLibjwsdp-2.0jaxwslibjsr181-api.jar;E:CommonLibjwsdp-2.0jaxwslibjsr250-api.jar;E:CommonLibjwsdp-2.0jaxblibjaxb-api.jar;E:CommonLibjwsdp-2.0jaxblibjaxb-impl.jar;E:CommonLibjwsdp-2.0jaxblibjaxb-xjc.jar;E:CommonLibjwsdp-2.0jaxblibjaxb1-impl.jar;
javac -d . fromjavaserver*.java
|
wsgen -cp . -keep -d . -wsdl fromjava.server.AddNumbersImpl
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
<web-app>
<display-name>fromjava</display-name>
<description>fromjava</description>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>fromjava</servlet-name>
<display-name>fromjava</display-name>
<description>JAX-WS endpoint - fromjava</description>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fromjava</servlet-name>
<url-pattern>/addnumbers</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app>
|
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>
<endpoint
name='fromjava'
implementation='fromjava.server.AddNumbersImpl'
url-pattern='/addnumbers'/>
</endpoints>
|
wsimport -d . -p addumber.client.gen http://localhost:8080/jaxws20test/addnumbers?wsdl
|
package fromjava.client;
import addumber.client.gen.*;
import addumber.client.gen.AddNumbersImpl;
public class AddNumbersClient {
public static void main (String[] args) {
try {
AddNumbersImpl port = new AddNumbersImplService().getAddNumbersImplPort();
int number1 = 10;
int number2 = 20;
System.out.printf ("Invoking addNumbers(%d, %d)n", number1, number2);
int result = port.addNumbers (number1, number2);
System.out.printf ("The result of adding %d and %d is %d.nn", number1, number2, result);
number1 = -10;
System.out.printf ("Invoking addNumbers(%d, %d)n", number1, number2);
result = port.addNumbers (number1, number2);
System.out.printf ("The result of adding %d and %d is %d.n", number1, number2, result);
} catch (AddNumbersException_Exception ex) {
System.out.printf ("Caught AddNumbersException_Exception: %sn", ex.getFaultInfo ().getDetail ());
}
}
}
|
java fromjava.client.AddNumbersClient
nvoking addNumbers(10, 20)
The result of adding 10 and 20 is 30.
Invoking addNumbers(-10, 20)
Caught AddNumbersException_Exception: Numbers: -10, 20
|