WebService:JAX-WS實現WebService

來源:互聯網
上載者:User

標籤:port   介面   cti   axis2   實現   nbsp   下載   java.net   orm   

        WebService和Java核心技術中的RMI一樣用於實現異構平台上的應用程式之間資料的互動,唯一不同的是這樣的技術屏蔽了語言之間的差異。這也是其大行其道的原因。

實現WebService的技術多種多樣,能夠使用JAX-WS、CXF、Axis2或Metro等方式實現WebService,接下來會給大家展示怎樣使用不同的方式實現WebService。本篇部落格為大家示範怎樣使用JAX-WS實現WebService:

        一、建立WebService伺服器端:

        1、建立一個名為“server”的Javaproject;

        2、建立IComputeService介面,代碼例如以下:

package com.ghj.service;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;/** * SEI Service Endpoint Interface 公布的服務介面 *  * @author GaoHuanjie */@WebService@SOAPBinding(style=SOAPBinding.Style.RPC)public interface IComputeService {public int add(int a, int b);}

        3、建立IComputeService介面實作類別ComputeService。代碼例如以下:

package com.ghj.service.impl;import javax.jws.WebService;import com.ghj.service.IComputeService;/** * SIB Service Implemention Bean  *  * @author GaoHuanjie *///endpointInterface指定存取點介面:介面必須存在 @WebService(endpointInterface="com.ghj.service.IComputeService")public class ComputeService implements IComputeService{@Overridepublic int add(int a, int b) {System.out.println(a+"+"+b+"="+(a+b));return a+b;}}

        4、建立啟動伺服器端服務的類StartServer,代碼例如以下:

package com.ghj.server;import javax.xml.ws.Endpoint;import com.ghj.service.impl.ComputeService;/** * 啟動伺服器端服務 *  * @author GaoHuanjie */public class StartServer {public static void main(String[] args) {String address = "http://localhost:8888/compute";Endpoint.publish(address, new ComputeService());}}

        二、建立WebService用戶端:

        1、建立一個名為“client”的Javaproject;

        2、將“server”project中的IComputeService介面複製到“client”project中;

        3、建立調用“server”projectIComputeService介面實作類別的代碼。代碼例如以下:

package com.ghj.client;import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;import com.ghj.service.IComputeService;public class Client {public static void main(String[] args) {try {URL url = new URL("http://localhost:8888/compute?wsdl");//建立訪問wsdl服務地址的urlQName sname = new QName("http://impl.service.ghj.com/", "ComputeServiceService");//通過Qname指明服務的詳細資料,其參數值與本project中的1.png圖Service service = Service.create(url,sname);//建立服務IComputeService computeService = service.getPort(IComputeService.class);//實現介面System.out.println(computeService.add(12,33));} catch (MalformedURLException e) {e.printStackTrace();}}}

        三、執行WebService:

        1、執行“server”project中的StartServer類;

        2、執行“client”project中的Client類。

        【0分下載資源

WebService:JAX-WS實現WebService

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.