The WebService tutorial explains of I in detail

  • 2020-05-07 19:36:53
  • OfStack

Web Services converts applications to network applications.

By using Web Services, your application can publish information to the world or provide a feature.

Web Services can be used by other applications.

With Web Services, your accounting department's Win 2k server can be connected to the UNIX server of the IT vendor.

The basic Web Services platform is XML+HTTP.

Web services USES XML to encode and decode data, and SOAP to transmit data.

what is webService

WebService, as its name implies, is a service based on Web. It USES Web(HTTP) to receive and respond to some kind of request from an external system. To achieve remote invocation.

1: from the working mode of WebService, it is not fundamentally different from ordinary Web programs (such as ASP, JSP, etc.), which are based on the HTTP transport protocol.

2: the data used by WebService is based on XML format. At present, standard WebService mainly adopts SOAP protocol in data format. SOAP is actually a text protocol based on XML encoding specification.

webService technical support

The Web Service platform requires a set of protocols to create distributed applications. Any platform has its data representation and type system. To achieve interoperability, the Web Service platform must provide a standard type system for communicating the different types of systems in different platforms, programming languages, and component models. The current agreements include:

XML and XSD

The extensible markup language XML is the basic format for representing data in the Web Service platform. In addition to being easy to build and easy to analyze, the main advantage of XML is that it is both platform and vendor agnostic. XML was created by the world wide web consortium (W3C). XML SchemaXSD, developed by W3C, defines a standard set of data types and gives a language to extend this set of data types.

The Web Service platform USES XSD as the data type system. When you construct an Web Service in a language such as VB.NET or C#, all the data types you use must be converted to XSD in order to comply with the Web Service standard. If you want it to be delivered across different platforms and different organizations with different software, you need something to wrap it around. This is a protocol such as SOAP.

SOAP

SOAP, the simple object access protocol (Simple Object Access Protocal), is a lightweight protocol for exchanging XML encoded information. It has three main aspects: XML-envelope defines a framework for describing the content of information and how to handle it, rules for coding program objects into XML objects, and conventions for performing remote procedure calls (RPC). SOAP can run on any other transport protocol. For example, it is 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 achieve the goal of "seamless integration based on Web" by enabling different systems to call each other in the way of "software-software dialogue", breaking the misfits among software applications, websites and various devices.

WSDL

Web Service description language WSDL is based on XML, a formal description document provided in a machine-readable manner, which describes Web Service and its functions, parameters, and return values. Because it is based on XML, WSDL is both machine-readable and human-readable.

UDDI

UDDI aims to establish standards for e-commerce; UDDI is a set of implementation standards for an information registry based on Web and distributed for Web Service. It also contains a set of implementation standards for access protocols that enable enterprises to register their own Web Service for other enterprises to discover. Call RPC with messaging

Web Service itself is actually implementing communication between applications. We now have two methods of application communication: RPC remote procedure call and messaging. With 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 transparency in the location: the server exposes the interface of the remote objects, and the client is like the interface of the objects used locally, thus hiding the underlying information, and the client does not need to know which machine the objects are on.

How do publish an WebService?

1. Release an WebService service with Jdk1.6.0_21 and view its wsdl documentation in the address bar.

2. Generate client code through wsimport, call it, and see the results of the run (learning how to call it is our focus).

It is important to note that when jdk releases WebService after jdk1.6._07, the code must be fully annotated. If you use jdk1.6.0_21 after jdk1.6.0_21, because it already contains ws2.1, you can only add the @WebService annotation to the class.

here are two different pieces of code:

released on jdk 1.6.0_13 ws:


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 support RPC Message style of  
public class HelloWorld { 
// The following through @WebMethod annotations , Public disclosure method  
@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 published on jdk1.6.0_24:


package com.itcast; 
import javax.jws.WebService; 
import javax.xml.ws.Endpoint; 
@WebService// Notice that's the only thing 1 A note , This note 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 1 a WebService Service application  
*/ 
// Through annotation , Indicate that such publication is 1 a WebService 
@WebService 
public class HelloWorld { 
public String sayHello(){ 
return "Hello World"; 
} 
// in main In the method , use javax.xml.ws.Endpoint The endpoint release 1 An application  
public static void main(String[] args) { 
Endpoint.publish("http://127.0.0.1:9999/helloworld", 
new HelloWorld()); 
} 
}

Code description: all non-static exposed methods of the HelloWorld class are exposed to the outside world.

Wsimport tool description:

The & # 61548; wsimport is a tool that comes with jdk and generates client-side invocation code from the wsdl documentation.

The & # 61548; wsimport.exe is located in the JAVA_HOME\bin directory.

The & # 61548; Common parameters of are:

• -d < directory > - the.class file will be generated. Default parameters.

• -s < directory > - the.java file will be generated.

• -p < The generated new package name > - places the generated class under the specified package.

(wsdlurl) - http: / / server: port/service & # 63; wsdl, required parameter

Use:

1: you can check your current version number with java and version. If the version is too low, you can install a higher version of jdk.

Or directly copy the installed jdk directory to your machine such as D:\ jdk1.6.0_24.

Because the previous environment variables have been set to the previous jdk directory, JAVA_HOME and PATH.

You can reset the environment variable to JAVA_HOME=D:\ jdk1.6.0_24,path=%JAVA_HOME%\bin,

After you reset the environment variable, you need to reopen an doc(command line) window.

If you do not want to modify the previously configured environment variables, you can enter the following command in the command line window to enable jdk1.6.0_24:

6.0 _24 set path = D: \ jdk1 \ bin; %PATH%(press enter)

Then check java and version to see if the version number of jdk has changed.

2: to go to a relatively clean directory, I created a new directory named ws on d disk and went to this directory.

3: open your webService.

4: enter the following command:

wsimport � s. http: / / 127.0.0.1:9999 / helloworld & # 63; wsdl

-s refers to the compilation of the source code file, later.

Back http... . Means to obtain the address of the wsdl specification.

5: at this point, the.java file and.class file will be generated (both containing the original package name).

6: in the new project, a new class (which can be under any package) calls the code generated above, see ppt on page 1.

7:wsimport other parameters, we often use the parameters of -d, -s, -p

-d < directory > The.class file will be generated.

Example: wsimport � d. http: / / 127.0.0.1:9999 / helloworld & # 63; wsdl

-s < directory > It will generate a.java file.

Example: wsimport � s. http: / / 127.0.0.1:9999 / helloworld & # 63; wsdl

-p < The package name > Changes 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 & # 63; wsdl

For the -p parameter, notice the change in the package name, which places the generated classes all under the package specified through -p (demo).

Note that when you use only the -p parameter, it also USES -d to compile the.class file.

RunMain.java source code is as follows:


package com.leaf; 
import com.itcast.HelloWorld; 
import com.itcast.HelloWorldService; 
/** 
*  By calling the generated class , To invoke the remote code  
*/ 
public class RunMain { 
public static void main(String[] args) { 
// from HelloWorldSerice the getHelloWorldPort Method returns the calling interface  
HelloWorld helloWorld = 
new HelloWorldService().getHelloWorldPort(); 
String str = helloWorld.sayHello(); // Execution call  
System.err.println(str);// return HelloWorld string  
} 
} 

The difference between WebService and normal Web programs

1, WebService only USES HTTP POST to transmit data, not GET;

1) Tttp post contentType

(1) application/x - www form -- urlencoded

2) contentType of WebService is

(2) Text/xml soap 1.1
(3) application/soap+xml

2 and WebService are limited in data transmission format.

The data used by WebService is based on the XML format. At present, standard WebService mainly adopts SOAP protocol in data format. The SOAP protocol is actually a text protocol based on the XML encoding specification.

WebService and web servers:

We can think of WebService as being used on Web servers; Conversely, the Web server is the container required for the WebService runtime. That's the difference and the connection.

WebService features:

1. WebService accepts the customer's request through HTTP POST

2. Between WebService and the client, SOAP protocol is used to transfer XML data.

3. It is designed to be cross-platform or cross-language.


Related articles: