01快速實現一個基於Jws的Webservice項目

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   os   

webservice

  異構平台之間的互動如:.net、php、python、perl

  流行的架構:CXF、Axis、Metro

  JAVA提供的:JAX-WS

一、快速實現一個基於Jws的Webservice項目

  1、伺服器的建立

    1.1建立介面

      

package com.bling.service;import javax.jws.WebService;@WebServicepublic interface IMyService {    public int sum(int a,int b);        public int minus(int a,int b);}

 

    1.2建立實作類別

package com.bling.service;import javax.jws.WebService;@WebService(endpointInterface="com.bling.service.IMyService")public class MyServiceImpl implements IMyService {    @Override    public int sum(int a, int b) {        // TODO Auto-generated method stub        System.out.println(a+"+"+b+" = "+(a+b));        return a+b;    }    @Override    public int minus(int a, int b) {        // TODO Auto-generated method stub        System.out.println(a+"+"+b+" = "+(a-b));        return a-b;    }}

    1.3開啟服務

package com.bling.service;import javax.xml.ws.Endpoint;public class MyService {    public static void main(String[] args) {        // TODO Auto-generated method stub        String address = "http://localhost:8888/ws";        Endpoint.publish(address, new MyServiceImpl());    }}

可以產生一個wsdl的檔案在服務地址中

  2.用戶端建立

    

package com.bling.service;import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;public class TestClient {    public static void main(String[] args) throws MalformedURLException {        // TODO Auto-generated method stub        URL url = new URL("http://localhost:8888/ws?wsdl");        QName sname= new QName("http://service.bling.com/","MyServiceImplService");        Service service = Service.create(url,sname);        IMyService ms = service.getPort(IMyService.class);        System.out.println(ms.sum(10, 20));        System.out.println(ms.minus(10, 20));    }}

輸出:

  30
  -10

GitBub源碼地址:https://github.com/WebServcie/service_start

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.