2011年10月25日 星期二

[Java] JAX-WS Example

Reference:
  1. JAX-WS Five Minute Tutorial
  2. JAX-WS Deployment Five Minute Tutorial
I cannot write this article without the above reference. Thank you very much, Mohammad Juma!

Run Under Eclipse:
  1. New a Dynamic Web Project.
  2. 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
    }
    
  3. 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;
        }
    }
    
  4. 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());
        }
    
    }
    
  5. 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.
  1. Execute
    cd YOUR_PROJECT_HOME
    wsgen -s src -d build/classes -cp build/classes test.TestServiceImpl
    
    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.
  2. 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>
    
  3. 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> 
    
  4. 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/.
  5. Export the whole project as a WAR file. 
  6. To run WAR files in Tomcat, refer to my blog.
  7. Link to http://localhost:8888/Test/TestService. Everything should be OK. The following is the soapUI screenshot:
Muchas gracias! Mohammad Juma!

沒有留言:

張貼留言