I cannot write this article without the above reference. Thank you very much, Mohammad Juma!
Run Under Eclipse:
- New a Dynamic Web Project.
- Create an endpoint interface:
package test; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface TestService { @WebMethod String sayHello(String name); @WebMethod Status getStatus(); }
package test; public enum Status { Started, Stopped }
- Implement the interface:
package test; import javax.jws.WebService; @WebService(endpointInterface = "test.TestService") public class TestServiceImpl implements TestService { @Override public String sayHello(String name) { return "Hello, Welcom to jax-ws " + name; } @Override public Status getStatus() { return Status.Started; } }
- Create endpoint publisher:
package test; import javax.xml.ws.Endpoint; public class Main { public static void main(String[] args) { Endpoint.publish("http://localhost:8888/TestService", new TestServiceImpl()); } }
- Run as Java application. Please check wsdl: http://localhost:8888/TestService?wsdl, or check http://localhost:8888/TestService. Here is the screenshot:
Deployment on Application Server:
Just follow the previous session except running. We need extra steps to complete our task.
- Execute
in the command prompt. We will see 4 class files (GetStatus, GetStatusResponse, SayHello and SayHelloResponse) in YOUR_PROJECT_HOME\build\classes\test\jaxws and 4 java files (the same) in YOUR_PROJECT_HOME\src\test\jaxws.cd YOUR_PROJECT_HOME wsgen -s src -d build/classes -cp build/classes test.TestServiceImpl
- Create web.xml under YOUR_PROJECT_HOME\WebContent\WEB-INF:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>TestService</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>TestService</servlet-name> <url-pattern>/TestService</url-pattern> </servlet-mapping> </web-app>
- Create sun-jaxws.xml under YOUR_PROJECT_HOME\WebContent\WEB-INF:
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="TestService" implementation="test.TestServiceImpl" url-pattern="/TestService"/> </endpoints>
- Copy all JARs under jaxws-ri-2.2\lib to YOUR_PROJECT_HOME\WebContent\WEB-INF\lib. To get the lastest JAX-WS reference implementation, visit http://jax-ws.java.net/.
- Export the whole project as a WAR file.
- To run WAR files in Tomcat, refer to my blog.
- Link to http://localhost:8888/Test/TestService. Everything should be OK. The following is the soapUI screenshot:
沒有留言:
張貼留言