What is a Web Service?
WebService is an SOA (service-oriented programming) architecture that is independent of language, platform-independent, and can be used to make calls to and from different languages, and to interact with HTTP protocol-based network applications over the Internet.
What is the use of WEB service?
Web service enables you to interact with programs written by two different languages and platforms, publish some services for others to use, or call some of the services that others have published.
What Web service needs to know:
SOAP: A communication protocol that sends XML-formatted data over HTTP.
WSDL: A description of the Web service used to describe the service
UDDL: The implementation standard specification for information registries, as well as a set of implementation standards that enable enterprises to register their own web service to enable other enterprises to discover the access protocol.
How to publish a service of your own (Web service)
There are several ways to publish and invoke Web service, only one of which
The code is as follows:
1 PackageCom.web.service;2 3 ImportJavax.jws.WebService;4 ImportJavax.xml.ws.Endpoint;5 6 @WebService7 Public classPublishService {8 //@webService Use annotations to define this class as a Web service note that the jdk1.6 and above versions are available9 Public Static voidMain (string[] args) {Ten //publish this class as a service endpoint with endpoint OneEndpoint.publish ("Http://localhost:6789/hi",NewPublishService ()); ASystem.out.println ("Ready ..."); - } - the Publicstring Sayhi (string name) { - return"Hello," +name; - } - //methods with static or final adornments are no longer displayed in WSDL + Public Staticstring SayHi1 (string name) { - return"Hello," +name; + } A Public Finalstring SayHi2 (string name) { at return"Hello," +name; - } -}
After running successfully open the browser access Service endpoint: HTTP://LOCALHOST:6789/HI?WSDL will display the following interface to illustrate the success of the publication:
This XML file does does appear to has any style information associated with it. The document tree is shown below.<!--Published by JAX-ws RI at http://jax-ws.dev.java.net. Ri's version is JAX-ws RI 2.2.4-B01.--><!--Generated by JAX-ws RI at http://jax-ws.dev.java.net. Ri's version is JAX-ws RI 2.2.4-B01.--><definitions xmlns:wsu= "http://docs.oasis-open.org/wss/2004/01/ Oasis-200401-wss-wssecurity-utility-1.0.xsd "xmlns:wsp=" Http://www.w3.org/ns/ws-policy "xmlns:wsp1_2="/HTTP/ Schemas.xmlsoap.org/ws/2004/09/policy "xmlns:wsam=" Http://www.w3.org/2007/05/addressing/metadata "xmlns:soap=" http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns=" http://service.web.com/"xmlns:xsd=" http://www.w3.org/2001/ XmlSchema "xmlns=" http://schemas.xmlsoap.org/wsdl/"targetnamespace=" http://service.web.com/"Name=" Publishserviceservice "><types><xsd:schema><xsd:Import Namespace= "http://service.web.com/" schemalocation= "http://localhost:6789/hi?xsd=1"/></xsd:schema>< /types><message name= "Sayhi" ><part name= "parameters" element= "Tns:sayhi"/></message>< Message name= "Sayhiresponse" ><part name= "parameters" element= "Tns:sayhiresponse"/></message>< PortType name= "PublishService" ><operation name= "Sayhi" ><input wsam:action= "http://service.web.com/ Publishservice/sayhirequest "message=" Tns:sayhi "/><output wsam:action=" http://service.web.com/ Publishservice/sayhiresponse "message=" Tns:sayhiresponse "/></operation></porttype><binding Name= "publishserviceportbinding" type= "Tns:publishservice" ><soap:binding transport= "http// Schemas.xmlsoap.org/soap/http "style=" document "/><operation name=" Sayhi "><soap:operation soapAction=" "/><input><soap:body use=" literal "/></input><output><soap:body use=" literal "/> </output></operatioN></binding><service name= "Publishserviceservice" ><port name= "PublishServicePort" binding= "TNS: Publishserviceportbinding "><soap:address location=" Http://localhost:6789/hi "/></port></ Service></definitions>
Once the publication is successful, we can invoke our service based on the service endpoint
How to invoke a Web Service:
The DOS command selects the drive letter to conveniently locate the file through the DOS command: wsimport-s. http://localhost:6789/hi?wsdl
Generate the client class source file as shown in the file:
Build successfully found this file
Create a new project copy this file in
Create a new test class
Reference in HTTP://LOCALHOST:6789/HI?WSDL
Creating an object from the service name and invoking the service port is called as follows:
1 Packagecom.web.test;2 3 ImportCom.web.service.PublishService;4 ImportCom.web.service.PublishServiceService;5 6 Public classServicetest {7 Public Static voidMain (string[] args) {8 //Create a service object and get the port9PublishService Port =NewPublishserviceservice (). Getpublishserviceport ();Ten //the Sayhi () method in the call port OneString hi = Port.sayhi ("Xiaoming"); A System.out.println (HI); - } -}
This enables the invocation of a Web service service in a different program
Note: When the publisher's server shuts down, the calling service will error
Their own summary, if there are errors, but also hope that you are not hesitate to enlighten, thank you.
WEB Service Essay