Webservice測試從頭來

來源:互聯網
上載者:User

標籤:

一、寫WebService

  建立java project

  建立資料類包 com.parameters.zz

    建立Person類     

package com.parameters.zz;public class Person {    private String name;    private String sex;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}
View Code

  建立服務類包 com.service.zz

    建立暴露介面類 ISayHello

      

package com.service.zz;import javax.jws.WebService;import com.parameters.zz.Person;@WebServicepublic class ISayHello {    private static final String SALUTATION = "Hello,";        public String getGreeting(Person p) {        String word="";        if(null!=p.getName()&&""!=p.getName().trim()){            if(null!=p.getSex()){                if("male".equalsIgnoreCase(p.getSex())){                    word=SALUTATION+"Mr."+p.getName();                }                else                    word=SALUTATION+"Miss."+p.getName();            }        }            return word;    } }
View Code

    建立啟動該服務類 ISayHelloService

package com.service.zz;import javax.xml.ws.Endpoint;public class ServiceRunning {    public static void main(String[] args) {        System.out.println("SayHello Web Service started.......");        Endpoint.publish("http://localhost:8098/ISayHelloService",                new ISayHello());    }}
View Code

  建立設定檔build.xml

    

<?xml version="1.0" encoding="UTF-8"?><project default="wsgen">          <target name="wsgen">          <exec executable="wsgen">                          <arg line="-cp ./bin -keep -s ./src -d ./bin                     com.service.zz.ISayHello"/>         </exec>     </target> </project>     
View Code

  整個項目路徑如題: 

  

  1.2 右鍵build.xml ,debug as “ant build”(alt+shift+d,q)

 

  1.3 開啟javaee視圖,開啟wsdl視圖

    

至此,Webservice發布成功,地址為 http://localhost:8098/ISayHelloService?wsdl

 

 

二、建立測試類別

  1.下載cxf2.2.8,並設定好環境變數

    

  2.進入dos,用cxf產生用戶端

    命令: wsdl2java –d . http://localhost:8098/ISayHelloService?wsdl  注意空格和“.”

    

    3.用Eclipse建立java project ,在src下匯入wsdl產生的用戶端

    

  4.編寫測試代碼:

    

測試代碼如果編譯通不過,可以換用JAXBElement

  如

Person p = new Person();        JAXBElement<String> name = new JAXBElement<String>(new QName("http://service.com", "name"), String.class, "George");        JAXBElement<String> sex = new JAXBElement<String>(new QName("http://service.com", "sex"), String.class, "male");        p.setName(name);        p.setSex(sex);
View Code

 

  

示範完畢,歡迎轉載:lenfengzhizhe,手邊星辰 

http://www.cnblogs.com/lengfengzhizhe/p/4218207.html

 

 

 

 

  

 

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.