webservice的簡單運用

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

Web Services是由企業發布的完成其特定商務需求的線上應用服務,其他公司或應用軟體能夠通過Internet來訪問並使用這項線上服務。

Web Service的關鍵技術和規則:

1.XML:描述資料的標準方法.

2.SOAP:表示資訊交換的協議(簡易物件存取通訊協定 (SOAP)).

3.WSDL:Web服務描述語言.

4.UDDI:通用描述、發現與整合,他是一種獨立於平台,基於XML語言的用於在互連網上描述商務的協議。

一、利用JDK web服務api實現,這裡使用基於SOAP message的Web Service:

1.首先建立一個Web Services項目

2.建立一個SayHello.java類,執行類,產生wsdl檔案

@WebService
public class SayHello {

    public String hello(String message){  
        return "Hello ," + message;  
    }  
      
    public static void main(String[] args) {  
        //create and publish an endPoint  
        SayHello hello = new SayHello();  
        Endpoint.publish("http://localhost:8080/hello", hello);  
    }  
}

可通過http://localhost:8080/hello?wsdl查看

三、執行dos命令執行:wsimport -p wsp.service -keep http://localhost:8080/hello?wsdl,組建檔案取.java檔案到項目中

  組建檔案:

粘貼到項目中的java檔案,用於調用

四、編寫調用類進行調用,注意:調用的服務是產生的,不是自己寫的

package wsp.main;

import wsp.service.SayHello;注意這兒
import wsp.service.SayHelloService;

public class Execute {

    public static void main(String[] args) {
        SayHelloService shs = new SayHelloService();
        SayHello sh = shs.getSayHelloPort();
        System.out.println(sh.hello("TOM"));
    }
}

中間出現的錯誤,原因可能是,包不對,注意產生java代碼的時候產生的包要和存到項目中的包名一樣,避免修改包名和不必要的麻煩

Exception in thread "main" javax.xml.ws.WebServiceException: Unable to create JAXBContext
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:156)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(AbstractSEIModelImpl.java:84)
    at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(RuntimeModeler.java:235)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.createSEIPortInfo(WSServiceDelegate.java:652)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.addSEI(WSServiceDelegate.java:640)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:332)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:315)
    at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:297)
    at javax.xml.ws.Service.getPort(Service.java:119)
    at wsp.service.SayHelloService.getSayHelloPort(SayHelloService.java:72)
    at wsp.main.Execute.main(Execute.java:10)
Caused by: java.security.PrivilegedActionException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://webservice.wsp/}hello". Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
        at wsp.service.Hello
        at public javax.xml.bind.JAXBElement wsp.service.ObjectFactory.createHello(wsp.service.Hello)
        at wsp.service.ObjectFactory
    this problem is related to the following location:
        at wsp.webservice.Hello
Two classes have the same XML type name "{http://webservice.wsp/}helloResponse". Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
        at wsp.service.HelloResponse
        at public wsp.service.HelloResponse wsp.service.ObjectFactory.createHelloResponse()
        at wsp.service.ObjectFactory
    this problem is related to the following location:
        at wsp.webservice.HelloResponse

    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(AbstractSEIModelImpl.java:143)
    ... 10 more
Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Two classes have the same XML type name "{http://webservice.wsp/}hello". Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
        at wsp.service.Hello
        at public javax.xml.bind.JAXBElement wsp.service.ObjectFactory.createHello(wsp.service.Hello)
        at wsp.service.ObjectFactory
    this problem is related to the following location:
        at wsp.webservice.Hello
Two classes have the same XML type name "{http://webservice.wsp/}helloResponse". Use @XmlType.name and @XmlType.namespace to assign different names to them.
    this problem is related to the following location:
        at wsp.service.HelloResponse
        at public wsp.service.HelloResponse wsp.service.ObjectFactory.createHelloResponse()
        at wsp.service.ObjectFactory
    this problem is related to the following location:
        at wsp.webservice.HelloResponse

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:283)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:126)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:173)
    at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(JAXBRIContext.java:96)
    at com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(JAXBContextFactory.java:98)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:151)
    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(AbstractSEIModelImpl.java:143)
    ... 12 more

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.