Detailed explanation of webService (1)

What is webService 
WebService, as the name suggests, is a Web-based service. It uses the Web (HTTP) method to receive and respond to certain requests from external systems. In order to realize remote call.  
1: From the working mode of WebService, it is not fundamentally different from ordinary Web programs (such as ASP, JSP, etc.), and they are all programs based on the HTTP transmission protocol.  
2: The data used by WebService is based on XML format. At present, the standard WebService mainly adopts the SOAP protocol in the data format. The SOAP protocol is actually a text protocol based on the XML encoding specification. 
The technical support of webService The 
web service platform needs a set of protocols to realize the creation of distributed applications. Any platform has its data representation and type system. To achieve interoperability, Web Services platforms must provide a standard type system for communicating different type systems across platforms, programming languages, and component models. Currently these protocols are: 
XML and XSD 
  Extensible Markup Language XML is the basic format for representing data in the Web Services platform. Besides being easy to build and easy to parse, the main advantage of XML is that it is both platform- and vendor-neutral. XML was created by the World Wide Web Consortium (W3C). The XML SchemaXSD developed by W3C defines a set of standard data types and provides a language to extend this data type.  
  The Web Service platform uses XSD as the data type system. When you construct a Web Service in a language such as VB. NET or C#, in order to conform to the Web Service standard, all data types you use must be converted to XSD types. If you want it to be passed between different organizations on different platforms and different software, you need to wrap it in something. That kind of thing is a protocol, like SOAP. 
 SOAP 
  SOAP stands for Simple Object Access Protocol, which is a lightweight protocol for exchanging XML-encoded information. It has three main aspects: XML-envelope defines the framework for describing the content of information and how to process it, the rules for encoding program objects into XML objects, and the conventions for implementing remote procedure calls (RPCs). SOAP can run over any other transport protocol. For example, it's tempting to use SMTP, the Internet e-mail protocol, to deliver SOAP messages. The headers are different between transport layers, but the XML payload remains the same.  
  Web Service hopes to realize that different systems can call each other in the way of "software-software dialogue", which breaks the incompatibility between software applications, websites and various devices, and achieves the goal of "seamless integration based on Web". 
 WSDL 
  Web Service Description Language WSDL is an XML-based language that provides a formal description document in a machine-readable way to describe Web Service and its functions, parameters and return values. Because it is based on XML, WSDL is both machine-readable and human-readable. 
 UDDI 
  The purpose of UDDI is to establish standards for e-commerce; UDDI is a set of Web-based, distributed, Web Service, and information registration center implementation standards. Service registration to enable other enterprises to discover the implementation standard of the access protocol. Calling RPC and messaging 
  Web Service itself is actually implementing communication between applications. We now have two methods of application communication: RPC remote procedure calls and message passing. When using RPC, the concept of the client is to invoke a remote procedure on the server, usually by instantiating a remote object and calling its methods and properties. The RPC system tries to achieve a kind of positional transparency: the server exposes the interface of the remote object, and the client seems to use the interface of these objects locally, so the underlying information is hidden, and the client does not need it at all. Know which machine the object is on. 
How to publish a WebService? 
1. Publish a WebService service with a later version of Jdk1.6.0_21. View its wsdl document through the address bar. 
2. Generate client code through wsimport, call and view the result of the operation. (Learning how to call is our focus) .It should be noted that 
when the jdk version after jdk1.6._07 publishes the WebService, the code must be fully annotated. If the version after jdk1.6.0_21 is used, because it already contains ws2.1, you can only use it. Add the annotation @WebService to the class. 
The following are two different pieces of code: 
ws released on the version of jdk1.6.0_13: 
package com.itcast; 
import javax.jws.WebMethod; 
import javax.jws.WebService; 
import javax. jws.soap.SOAPBinding; 
import javax.jws.soap.SOAPBinding.Style; 
import javax.xml.ws.Endpoint; 
@WebService(targetNamespace=" http://loalhost:9999/helloworld ") 
@SOAPBinding(style=Style.RPC)//Only supports RPC message style 
public class HelloWorld { 
//The following is annotated with @WebMethod to expose methods 
@ WebMethod 
public String sayHello(){ 
return "HelloWorld"; 

public static void main(String[] args) { 
Endpoint.publish(" http://localhost:9999/helloworld",new  HelloWorld()); 


2: The following is the WebService code released on jdk1.6.0_24: 
package com.itcast; 
import javax.jws.WebService; 
import javax.xml.ws.Endpoint; 
@WebService//Note that there is only one annotation, this annotation is also required, The default SOAP message style is: DOCUMENT 
public class HelloWorld { 
public String sayHello(){ 
return "HelloWorld"; 

public static void main(String[] args) { 
Endpoint.publish(" http://localhost:9999/helloworld",new  HelloWorld()); 


The first WebService service 
package com.itcast ; 
import javax.jws.WebService; 
import javax.xml.ws.Endpoint; 
/** 
 * The first WebService service application 
 */ 
//Through annotation, mark this class as a WebService 
@WebService 
public class HelloWorld { 
public String sayHello (){ 
return "Hello World"; 

//In the main method, use the javax.xml.ws.Endpoint endpoint to publish an application 
public static void main(String[] args) { 
Endpoint.publish(" http://127.0 .0.1:9999/helloworld ", 
 new HelloWorld()); 


Code description: All non-static public methods of the HelloWorld class will be exposed to the outside world. 
Wsimport tool description: 
 wsimport is a tool that comes with jdk, and can generate client calling code according to wsdl documents. Of course, No matter what language the WebService on the server side is written in, Java code will be generated on the client side. It doesn't matter what the server side is written in. 
 wsimport.exe is located in the JAVA_HOME\bin directory. 
 Common parameters are: 
• -d< Directory > - will generate .class files. Default parameters. 
• -s <directory> - .java files will be generated. 
• -p<generated new package name> - Put the generated classes under the specified package. 
(wsdlurl) -  http://server:port/service?wsdl , the required parameters are 
used: 
1: You can check your current version number through java –version. If the version is too low, you can install a higher version of jdk. 
Or directly put others Copy the installed jdk directory to your machine such as D:\jdk1.6.0_24 directory. 
Because the previous environment variable has been set to the previous jdk directory of the old version, namely JAVA_HOME and PATH two environment variables. 
You can reset it again The following environment variables are: JAVA_HOME=D:\jdk1.6.0_24, path=%JAVA_HOME%\bin, 
after resetting the environment variables, you need to reopen a doc (command line) window to take effect. 
If you do not want to modify the previously configured environment variables, you can enter the following command in the command line window to make jdk1.6.0_24 take effect: 
set path = D:\jdk1.6.0_24\bin;%PATH% (press Enter) 
and then pass java –version to see if the version number of jdk has changed. 
2: Go to a relatively clean directory, I created a new directory on the d drive named: ws, and go to this directory. 
3: Open your webService. 
4: Enter the following command: 
  wsimport –s .  http://127.0.0.1:9999/helloworld?wsdl    Parameter description: -s means to compile the source code file, the following .(dot) means to put the code in the current Under the directory.     The last http.... refers to the address to obtain the wsdl manual.  5: At this point, the .java file and the .class file will be generated. (both contain the original package name). Copy the code to your project. ( Only copy the java file)  6: In a new project, a new class, (can be located in any package), call the code generated above, see ppt on the next page.  7: wsimport other parameter description, we often use Parameters -d, -s, -p  -d <directory> will generate .class files.  Example: wsimport -d . http://127.0.0.1:9999/helloworld?wsdl -s <directory> will be generated. java file.  Example: wsimport –s . http://127.0.0.1:9999/helloworld?wsdl 






  

  
-p <package name> Modify the generated file (.java or .class to the specified package name) 
Example: wsimport -s . -p com.beijing.itcast  http://127.0.0.1:9999/helloworld?wsdl For the -p parameter, pay attention to the modification of the package name, it will place all the generated classes under the package specified by -p. (Demo)  It should be noted that when only the -p parameter is used, it will also use - d is compiled into a .class file. If the –d parameter is written or not, it is all there, and it is always there.  The source code of RunMain.java is as follows:  package com.leaf;  import com.itcast.HelloWorld;  import com.itcast. HelloWorldService;  /**   * Call the remote code by calling the generated class   */  public class RunMain {  public static void main(String[] args) {  //Return the calling interface from the getHelloWorldPort method of  HelloWorldSerice HelloWorld helloWorld =   new HelloWorldService( ).getHelloWorldPort();  String str = helloWorld.sayHello(); //Execute call  System.err.println(str);//Return HelloWorld string  


















The difference between WebService and ordinary Web programs 
1. WebService only uses HTTP POST to transmit data, not GET;  
1) The contentType of Tttp post is 
(1) application/x-www-form-urlencoded 
2) The contentType of WebService is 
(2) Text/xml soap1.1 
(3) application/soap+xml-soap1.2 
2. WebService is limited from the data transmission format. The data used by WebService is based on XML format. At present, the standard WebService mainly adopts the SOAP protocol in the data format. The SOAP protocol is actually a text protocol based on the XML encoding specification. The difference between WebService and web server:  We can think of WebService as an application on a Web server; on the other hand, a Web server is a necessary container for WebService to run. This is their difference and connection. Features of WebService:  1. WebService accepts client's request through HTTP POST.  2. SOAP protocol is generally used to transmit XML data between WebService and client.  3. It is designed for cross-platform or cross-language.
 


 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484810&siteId=291194637