webservice的简单运用
Web Services是由企业发布的完成其特定商务需求的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。
Web Service的关键技术和规则:
1.XML:描述数据的标准方法.
2.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