標籤: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的簡單運用