溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用JDK1.6的JAX-WS編寫WebService

發(fā)布時(shí)間:2021-12-04 14:06:49 來源:億速云 閱讀:150 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)如何使用JDK1.6的JAX-WS編寫WebService,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

一、Web services概念

Web services是客戶端和服務(wù)端通過萬維網(wǎng)的HTTP協(xié)議進(jìn)行交互。

二、JAX-WS實(shí)現(xiàn)簡(jiǎn)單的Web services

2.1 建一個(gè)名為HelloServer的Web應(yīng)用作為Webservice客戶端

2.2 在HelloServer應(yīng)用下新建一個(gè)類:

package helloservice.endpoint; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class Hello {     private String message = new String("Hello, ");     public void Hello() {     }     @WebMethod     public String sayHello(String name) {         return message + name + ".";      } }

2.3 在weblogic下發(fā)布HelloServer應(yīng)用,應(yīng)用名為WebRoot。

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

如果可以查看到wsdl的內(nèi)容說明發(fā)布成功.比如:

  <?xml version="1.0" encoding="UTF-8" ?>  - <!--  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).    -->  - <!--  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).    -->  - <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://endpoint.helloservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://endpoint.helloservice/" name="HelloService"> - <types> - <xsd:schema>   <xsd:import namespace="http://endpoint.helloservice/" schemaLocation="http://localhost:7001/WebRoot/HelloService?xsd=1" />    </xsd:schema>   </types> - <message name="sayHello">   <part name="parameters" element="tns:sayHello" />    </message> - <message name="sayHelloResponse">   <part name="parameters" element="tns:sayHelloResponse" />    </message> - <portType name="Hello"> - <operation name="sayHello">   <input message="tns:sayHello" />    <output message="tns:sayHelloResponse" />    </operation>   </portType> - <binding name="HelloPortBinding" type="tns:Hello">   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />  - <operation name="sayHello">   <soap:operation soapAction="" />  - <input>   <soap:body use="literal" />    </input> - <output>   <soap:body use="literal" />    </output>   </operation>   </binding> - <service name="HelloService"> - <port name="HelloPort" binding="tns:HelloPortBinding">   <soap:address location="http://localhost:7001/WebRoot/HelloService" />    </port>   </service>   </definitions>

2.5 運(yùn)行wsimport

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

2.5.1 在E:\Program Files\PowerCmd>目錄下,新建一個(gè)文件夾generate。

2.5.2 運(yùn)行如下命令:

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

如果返回

parsing WSDL...
generating code...

說明運(yùn)行成功。

2.5.3 查看generate目錄,可以看到生成了JAVA文件,與generate同級(jí)的目錄下,還有class文件。(這里生成的JAVA文件,客戶端需要用到)

生成的HelloService.java如下:

package helloservice.endpoint;  import java.net.MalformedURLException;  import java.net.URL;  import javax.xml.namespace.QName;  import javax.xml.ws.Service;  import javax.xml.ws.WebEndpoint;  import javax.xml.ws.WebServiceClient;  import javax.xml.ws.WebServiceFeature;  /**   * This class was generated by the JAX-WS RI.   * JAX-WS RI 2.1.1 in JDK 6   * Generated source version: 2.1   *    */  @WebServiceClient(name = "HelloService", targetNamespace = "http://endpoint.helloservice/", wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl")  public class HelloService      extends Service  {      private final static URL HELLOSERVICE_WSDL_LOCATION;      static {          URL url = null;          try {              url = new URL("http://localhost:7001/WebRoot/HelloService?wsdl");          } catch (MalformedURLException e) {              e.printStackTrace();          }          HELLOSERVICE_WSDL_LOCATION = url;      }        public HelloService(URL wsdlLocation, QName serviceName) {          super(wsdlLocation, serviceName);      }        public HelloService() {          super(HELLOSERVICE_WSDL_LOCATION, new QName("http://endpoint.helloservice/", "HelloService"));      }      /**       *        * @return       *     returns Hello       */      @WebEndpoint(name = "HelloPort")      public Hello getHelloPort() {          return (Hello)super.getPort(new QName("http://endpoint.helloservice/", "HelloPort"), Hello.class);      }      /**       *        * @param features       *     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.       * @return       *     returns Hello       */      @WebEndpoint(name = "HelloPort")      public Hello getHelloPort(WebServiceFeature... features) {          return (Hello)super.getPort(new QName("http://endpoint.helloservice/", "HelloPort"), Hello.class, features);      }  }

2.6 建一個(gè)名為HelloClient的Web應(yīng)用作為WebService客戶端。

2.7 將3.5.3生成的JAVA文件復(fù)制到HelloClient的src下。

2.8 新建一個(gè)HelloServlet文件,如下:

package webclient; import helloservice.endpoint.HelloService; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.ws.WebServiceRef;  @WebServlet(name = "HelloServlet", urlPatterns = { "/HelloServlet" }) public class HelloServlet extends HttpServlet {     @WebServiceRef(wsdlLocation = "http://localhost:7001/WebRoot/HelloService?wsdl")     private HelloService service;      /**      * Constructor of the object.      */     public HelloServlet() {         super();     }     /**      * Destruction of the servlet. <br>      */     public void destroy() {         super.destroy(); // Just puts "destroy" string in log         // Put your code here     }     /**      * The doGet method of the servlet. <br>      *      * This method is called when a form has its tag value method equals to get.      *       * @param request the request send by the client to the server      * @param response the response send by the server to the client      * @throws ServletException if an error occurred      * @throws IOException if an error occurred      */     public void doGet(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         processRequest(request, response);     }     /**      * The doPost method of the servlet. <br>      *      * This method is called when a form has its tag value method equals to post.      *       * @param request the request send by the client to the server      * @param response the response send by the server to the client      * @throws ServletException if an error occurred      * @throws IOException if an error occurred      */     public void doPost(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         processRequest(request, response);     }     /**      * Initialization of the servlet. <br>      *      * @throws ServletException if an error occurs      */     public void init() throws ServletException {         // Put your code here     }          /**      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>      * methods.      *       * @param request      *            servlet request      * @param response      *            servlet response      * @throws ServletException      *             if a servlet-specific error occurs      * @throws IOException      *             if an I/O error occurs      */     protected void processRequest(HttpServletRequest request,             HttpServletResponse response) throws ServletException, IOException {         response.setContentType("text/html;charset=UTF-8");         PrintWriter out = response.getWriter();         try {             out.println("<html lang=\"en\">");             out.println("<head>");             out.println("<title>Servlet HelloServlet</title>");             out.println("</head>");             out.println("<body>");             out.println("<h2>Servlet HelloServlet at "                     + request.getContextPath() + "</h2>");             out.println("<p>" + sayHello("world") + "</p>");             out.println("</body>");             out.println("</html>");         } finally {             out.close();         }     }     // doGet and doPost methods, which call processRequest, and     // getServletInfo method     private String sayHello(java.lang.String arg0) {         helloservice.endpoint.Hello port = service.getHelloPort();         return port.sayHello(arg0);     } }

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

<servlet>     <description>HelloServlet</description>      <display-name>HelloServlet</display-name>     <servlet-name>HelloServlet</servlet-name>     <servlet-class>webclient.HelloServlet</servlet-class>   </servlet> <servlet-mapping>     <servlet-name>HelloServlet</servlet-name>     <url-pattern>/servlet/HelloServlet</url-pattern>   </servlet-mapping>

2.10 發(fā)布HelloClient應(yīng)用。

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

頁面內(nèi)容如下說明WebService調(diào)用成功!

如何使用JDK1.6的JAX-WS編寫WebService

關(guān)于“如何使用JDK1.6的JAX-WS編寫WebService”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI