JDK1.6的JAX-WS编写WebService【转】 生成客户端代码

来源:互联网 发布:最简单的java代码 编辑:程序博客网 时间:2024/05/27 18:17
 

使用JDK1.6的JAX-WS编写WebService

JAX-WS规范是一组XML web services的JAVA API。JAX-WS允许开发者可以选择RPC-oriented或者message-oriented 来实现自己的web services。下面我们就用JAX-WS实现简单的Web services吧。

AD:


    一、Web services概念

    Web services是客户端和服务端通过万维网的HTTP协议进行交互。

    二、JAX-WS实现简单的Web services

    2.1 建一个名为HelloServer的Web应用作为Webservice客户端

    2.2 在HelloServer应用下新建一个类:

    1. package helloservice.endpoint; 
    2. import javax.jws.WebMethod; 
    3. import javax.jws.WebService; 
    4. @WebService 
    5. public class Hello { 
    6.     private String message = new String("Hello, "); 
    7.     public void Hello() { 
    8.     } 
    9.     @WebMethod 
    10.     public String sayHello(String name) { 
    11.         return message + name + ".";  
    12.     } 

    2.3 在weblogic下发布HelloServer应用,应用名为WebRoot。

    2.4 在IE里面打开http://localhost:7001/WebRoot/HelloService?wsdl

    如果可以查看到wsdl的内容说明发布成功.比如:

    1.   <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!--  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.3-07/10/2008 08:41 PM(bt).  
    3.   -->  
    4. <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.3-07/10/2008 08:41 PM(bt).  
    5.   -->  
    6. <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    7.  xmlns:tns="http://endpoint.helloservice/"
    8.  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    9.  xmlns="http://schemas.xmlsoap.org/wsdl/"
    10.  targetNamespace="http://endpoint.helloservice/"
    11.  name="HelloService"> 
    12. <types> 
    13. <xsd:schema> 
    14.   <xsd:import namespace="http://endpoint.helloservice/" schemaLocation="http://localhost:7001/WebRoot/HelloService?xsd=1" />  
    15.   </xsd:schema> 
    16.   </types> 
    17. <message name="sayHello"> 
    18.   <part name="parameters" element="tns:sayHello" />  
    19.   </message> 
    20. <message name="sayHelloResponse"> 
    21.   <part name="parameters" element="tns:sayHelloResponse" />  
    22.   </message> 
    23. <portType name="Hello"> 
    24. <operation name="sayHello"> 
    25.   <input message="tns:sayHello" />  
    26.   <output message="tns:sayHelloResponse" />  
    27.   </operation> 
    28.   </portType> 
    29. <binding name="HelloPortBinding" type="tns:Hello"> 
    30.   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />  
    31. <operation name="sayHello"> 
    32.   <soap:operation soapAction="" />  
    33. <input> 
    34.   <soap:body use="literal" />  
    35.   </input> 
    36. <output> 
    37.   <soap:body use="literal" />  
    38.   </output> 
    39.   </operation> 
    40.   </binding> 
    41. <service name="HelloService"> 
    42. <port name="HelloPort" binding="tns:HelloPortBinding"> 
    43.   <soap:address location="http://localhost:7001/WebRoot/HelloService" />  
    44.   </port> 
    45.   </service> 
    46.   </definitions> 

    2.5 运行wsimport

    wsimport是JDK1.6特有的,[JAVA_HOME]/bin下。

    2.5.1 在E:\Program Files\PowerCmd>目录下,新建一个文件夹generate。

    2.5.2 运行如下命令:

    wsimport -s generate http://localhost:7001/WebRoot/HelloService?wsdl

    如果返回

    parsing WSDL...generating code...

    说明运行成功。

    2.5.3 查看generate目录,可以看到生成了JAVA文件,与generate同级的目录下,还有class文件。(这里生成的JAVA文件,客户端需要用到)

    生成的HelloService.java如下:

    1. package helloservice.endpoint;  
    2. import java.net.MalformedURLException;  
    3. import java.net.URL;  
    4. import javax.xml.namespace.QName;  
    5. import javax.xml.ws.Service;  
    6. import javax.xml.ws.WebEndpoint;  
    7. import javax.xml.ws.WebServiceClient;  
    8. import javax.xml.ws.WebServiceFeature;  
    9. /**  
    10.  * This class was generated by the JAX-WS RI.  
    11.  * JAX-WS RI 2.1.1 in JDK 6  
    12.  * Generated source version: 2.1  
    13.  *   
    14.  */  
    15. @WebServiceClient(name = "HelloService", targetNamespace = "http://endpoint.helloservice/", wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl")  
    16. public class HelloService  
    17.     extends Service  
    18. {  
    19.     private final static URL HELLOSERVICE_WSDL_LOCATION;  
    20.     static {  
    21.         URL url = null;  
    22.         try {  
    23.             url = new URL("http://localhost:7001/WebRoot/HelloService?wsdl");  
    24.         } catch (MalformedURLException e) {  
    25.             e.printStackTrace();  
    26.         }  
    27.         HELLOSERVICE_WSDL_LOCATION = url;  
    28.     }  
    29.   
    30.     public HelloService(URL wsdlLocation, QName serviceName) {  
    31.         super(wsdlLocation, serviceName);  
    32.     }  
    33.   
    34.     public HelloService() {  
    35.         super(HELLOSERVICE_WSDL_LOCATION, new QName("http://endpoint.helloservice/""HelloService"));  
    36.     }  
    37.     /**  
    38.      *   
    39.      * @return  
    40.      *     returns Hello  
    41.      */  
    42.     @WebEndpoint(name = "HelloPort")  
    43.     public Hello getHelloPort() {  
    44.         return (Hello)super.getPort(new QName("http://endpoint.helloservice/""HelloPort"), Hello.class);  
    45.     }  
    46.     /**  
    47.      *   
    48.      * @param features  
    49.      *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.  
    50.      * @return  
    51.      *     returns Hello  
    52.      */  
    53.     @WebEndpoint(name = "HelloPort")  
    54.     public Hello getHelloPort(WebServiceFeature... features) {  
    55.         return (Hello)super.getPort(new QName("http://endpoint.helloservice/""HelloPort"), Hello.class, features);  
    56.     }  
    57. }  

    2.6 建一个名为HelloClient的Web应用作为WebService客户端。

    2.7 将3.5.3生成的JAVA文件复制到HelloClient的src下。

    2.8 新建一个HelloServlet文件,如下:

    1. package webclient; 
    2. import helloservice.endpoint.HelloService; 
    3. import java.io.IOException; 
    4. import java.io.PrintWriter; 
    5. import javax.servlet.ServletException; 
    6. import javax.servlet.annotation.WebServlet; 
    7. import javax.servlet.http.HttpServlet; 
    8. import javax.servlet.http.HttpServletRequest; 
    9. import javax.servlet.http.HttpServletResponse; 
    10. import javax.xml.ws.WebServiceRef; 
    11.  
    12. @WebServlet(name = "HelloServlet", urlPatterns = { "/HelloServlet" }) 
    13. public class HelloServlet extends HttpServlet { 
    14.     @WebServiceRef(wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl"
    15.     private HelloService service; 
    16.  
    17.     /** 
    18.      * Constructor of the object. 
    19.      */ 
    20.     public HelloServlet() { 
    21.         super(); 
    22.     } 
    23.     /** 
    24.      * Destruction of the servlet. <br> 
    25.      */ 
    26.     public void destroy() { 
    27.         super.destroy(); // Just puts "destroy" string in log 
    28.         // Put your code here 
    29.     } 
    30.     /** 
    31.      * The doGet method of the servlet. <br> 
    32.      * 
    33.      * This method is called when a form has its tag value method equals to get. 
    34.      *  
    35.      * @param request the request send by the client to the server 
    36.      * @param response the response send by the server to the client 
    37.      * @throws ServletException if an error occurred 
    38.      * @throws IOException if an error occurred 
    39.      */ 
    40.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
    41.             throws ServletException, IOException { 
    42.         processRequest(request, response); 
    43.     } 
    44.     /** 
    45.      * The doPost method of the servlet. <br> 
    46.      * 
    47.      * This method is called when a form has its tag value method equals to post. 
    48.      *  
    49.      * @param request the request send by the client to the server 
    50.      * @param response the response send by the server to the client 
    51.      * @throws ServletException if an error occurred 
    52.      * @throws IOException if an error occurred 
    53.      */ 
    54.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
    55.             throws ServletException, IOException { 
    56.         processRequest(request, response); 
    57.     } 
    58.     /** 
    59.      * Initialization of the servlet. <br> 
    60.      * 
    61.      * @throws ServletException if an error occurs 
    62.      */ 
    63.     public void init() throws ServletException { 
    64.         // Put your code here 
    65.     } 
    66.      
    67.     /** 
    68.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
    69.      * methods. 
    70.      *  
    71.      * @param request 
    72.      *            servlet request 
    73.      * @param response 
    74.      *            servlet response 
    75.      * @throws ServletException 
    76.      *             if a servlet-specific error occurs 
    77.      * @throws IOException 
    78.      *             if an I/O error occurs 
    79.      */ 
    80.     protected void processRequest(HttpServletRequest request, 
    81.             HttpServletResponse response) throws ServletException, IOException { 
    82.         response.setContentType("text/html;charset=UTF-8"); 
    83.         PrintWriter out = response.getWriter(); 
    84.         try { 
    85.             out.println("<html lang=\"en\">"); 
    86.             out.println("<head>"); 
    87.             out.println("<title>Servlet HelloServlet</title>"); 
    88.             out.println("</head>"); 
    89.             out.println("<body>"); 
    90.             out.println("<h1>Servlet HelloServlet at " 
    91.                     + request.getContextPath() + "</h1>"); 
    92.             out.println("<p>" + sayHello("world") + "</p>"); 
    93.             out.println("</body>"); 
    94.             out.println("</html>"); 
    95.         } finally { 
    96.             out.close(); 
    97.         } 
    98.     } 
    99.     // doGet and doPost methods, which call processRequest, and 
    100.     // getServletInfo method 
    101.     private String sayHello(java.lang.String arg0) { 
    102.         helloservice.endpoint.Hello port = service.getHelloPort(); 
    103.         return port.sayHello(arg0); 
    104.     } 

    2.9 配置HelloClient的Web.xml,增加如下代码:

    1. <servlet> 
    2.     <description>HelloServlet</description>  
    3.     <display-name>HelloServlet</display-name> 
    4.     <servlet-name>HelloServlet</servlet-name> 
    5.     <servlet-class>webclient.HelloServlet</servlet-class> 
    6.   </servlet> 
    7. <servlet-mapping> 
    8.     <servlet-name>HelloServlet</servlet-name> 
    9.     <url-pattern>/servlet/HelloServlet</url-pattern> 
    10.   </servlet-mapping> 

    2.10 发布HelloClient应用。

    2.11 在IE录入http://localhost:7111/servlet/HelloServlet

    页面内容如下说明WebService调用成功!

    原文链接:http://maymay.iteye.com/blog/1271139

    原创粉丝点击