A tentative study of webservice (simple small demo)

Source: Internet
Author: User
Tags http post webservice annotation wsdl

1. What is WebService? 1.1. First, the benefits:
    1. WebService is a remote invocation of two systems that enables data interaction between two systems, such as: Weather forecasting services, bank ATM withdrawals, using email accounts to log in to websites, and more.
    2. The call between WebService is a cross-language call. Java,. Net, PHP, send HTTP requests, the data format used is XML format.
    3. webxml.com.cn There are some free webservice services, you can go inside to see.

  Basic concepts:  

(1) Understanding services:

Today's applications are becoming more complex, and even a single application cannot do all the work. Not to mention the use of only one language. So you need access to services written by others to get the data you're interested in.

When writing applications to query the database, and did not consider why the query results can be returned to the upper layer of the application, and even think that this is what the database should do, in fact, this is the database through the TCP/IP protocol with the other application of the results of communication, and the upper layer is what kind of application, is in what language, the database itself does not know, it only know to receive an agreement, this is the SQL92 query standard protocol.

Today's cloud computing, Cloud Avira is a service, now more popular is the term SOA (service-oriented framework).

Why can't we implement cross-platform, cross-language services, since the database can serve other external applications according to certain criteria and doesn't care what language each other uses?

As long as we write code in Java, can be called by any language, we realize the cross-platform, cross-language service! ---WebService

Therefore, WebService, as the name implies, is a web-based service. It uses the Web (HTTP) method to receive and respond to some kind of request from the external system. This allows for remote invocation.

We can call the Internet to query the Weather information Web service, and then embed it in our program (c/S or b/S program), when the user from our network to see the weather information, he will think we provide him with a lot of information services, but in fact we did nothing, Just a simple call to a piece of code on the server.

Learning WebService can publish your service (a piece of code) to the Internet for others to call, or you can call the WebService published on someone else's machine, just as you would with your own code.

(2) Basic concept: XML

XML Extensible Markup Language-Extensible Markup Language

XML, which is used to transfer formatted data, is the foundation of Web services.

The namespace-namespace.

(3) Basic concept: WSDL

Wsdl–webservice Description language–web Service Description Language.

Explain where the service is-address, in XML form. Address location

Describe what method the service provides in XML form – how to invoke it. Operation

(4) Basic concept: SOAP

Soap-simple Object access Protocol (Simple Object Accessing protocol)

Soap is used as an XML-based protocol for transmitting data on the Web.

SOAP = +xml data on the basis of HTTP.

SOAP is HTTP-based.

The composition of soap is as follows:

envelope– must be part of. appears as the root element of the XML.

headers– is optional.

body– is necessary. In the body section, the method that contains the server to be executed. And the data sent to the server.

The data format passed:

1 <Envelope>2 3       <Header></Header>4 5       <Body>6 7                 <Method Name>8 9 Method ParametersTen  One           </Method Name> A  -       </Body> -  the </Envelope>

(5) Sample request:

The following HTTP requests are made, but the difference is that the XML data is sent to the server!

Note: (1), because the data is sent on HTTP, you must first follow the HTTP protocol

(2), the XML part, the SOAP protocol, must contain the envelope and body elements.

(6) Example of response:

   

1.2. Application Notes

1,webservice remote invocation via HTTP protocol

(1), the WebService only uses the HTTP POST method to transmit the data, does not use the Get method; --Handshake, Wsdl-get, (based on the SOAP protocol, the transfer data format is XML)

The contenttype of the normal HTTP post is

application/x-www-form-urlencoded

WebService ContentType--that is, the SOAP protocol based on HTTP

Text/xml This is based on the soap1.1 protocol.

Application/soap+xml This is based on the soap1.2 protocol.

(2), the webservice from the data transmission format is limited. The data used by WebService is based on XML format. The current standard webservice mainly uses the SOAP protocol in the data format. The SOAP protocol is actually a text protocol based on XML encoding specification.

(3), Soap–simple Object Access Protocol simple to the like Access Protocol. is a protocol that runs on top of the HTTP protocol. In fact, the HTTP protocol is the transfer of XML files, it becomes the SOAP protocol.

(4), SOAP1.1 and SOAP1.2 are not the same namespace. You can view the class by

Javax.xml.ws.soap.SOAPBinding to see the constants inside.

By default, Jdk1.6 only supports soap1.1

namely: @BindingType (value=javax.xml.ws.soap.soapbinding.soap11http_binding)

1.3. The difference and connection between WebService and the Web

You can think of WebService as an application on a Web server, and conversely, a Web server is a necessary container for the WebService runtime. This is their difference and connection.

1.4. Characteristics of WebService

WebService accepts customer requests via HTTP POST (if the Transfer data format is XML based on the SOAP protocol), only the Post method, because the Get method has no request body.

The SOAP protocol is generally used to transfer XML data between WebService and the client.

It is designed for cross-platform or cross-language.

(1) SOAP1.2 Note: When SOAP12 is used, Wsimport and Eclipse and Wsexplorer are not working properly, and local code must be generated using the CXF tool provided by Wsdl2java.

(2) The client is best to send 1.1 requests, and the server is best to use the 1.2 high version.

2. Simple examples

  

2.1. Publish a WebService service with jdk1.6.0_21 later (using annotations)

The classes related to Web services are located in the javax.jws.* package.

The main classes are:

@WebService-it is an annotation that is specified on the class to publish this class as a WS.

Endpoint– This class is the endpoint service class, and its method publish is used to bind an @webservice annotation object that has been added to an address on the port.

(1) A simple Java project, WebService

  ① set up the following package structure:

    

      

② creates a new class Hellowebservice.java with the main method and adds a @webservice comment to the class.

Use the Publish method of the endpoint class in your class:

You also need to provide at least one method that can be published: (the method cannot be static and non-final), only such a method can be published.

1  PackageCn.jingyangxu.service;2 3 ImportJavax.jws.WebService;4 ImportJavax.xml.ws.Endpoint;5 6 @WebService7  Public classHellowebservice {8     9      Publicstring Test (string name) {Ten         return"I call:" +name; One     } A      -      Public Static voidmain (String [] args) { -Endpoint.publish ("Http://localhost:8899/hello",NewHellowebservice ()); theSystem.out.println ("Start ...") ; -     } -      -}

  

After the ③ service is published successfully, it is called on the client:

After starting the service, enter the binding service address + "? wsdl" in the browser to view the service's manual. Wsdl-webservice Description Language, is in the form of XML file to describe the WebService "instructions", with the instructions, we can know how to use or invoke the service.

④ use Wsimport–s. http://localhost:8899/hello?wsdl

To generate the client code. (Contains. class files and. java files)

Note here: It is generated instead of downloaded, and all classes and methods are not generated on the server.

⑤ Create a new Java project client, and put the. java file package together under this project, call:

        

Success!

3. Description

Wsimport is the JDK that comes with the client calling code that can be generated from the WSDL document. Of course, the Java code will be generated on the client, regardless of the language that the server-side webservice is written in. It doesn't matter what the server uses to write.

Wsimport.exe is located in the Java_home\bin directory.

Common parameters are:

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

-s< directory >-A. java file will be generated.

-p< generated new package name >-The generated class is placed under the specified package, customizing the package structure.

(wsdlurl)-http://server:port/service?wsdl, required parameter.

Example:

C:/> Wsimport–s. http://192.168.0.100/one?wsdl

Note:-S cannot be separated, and there is a small dot behind-s to specify the source code generated directory. The point is the current directory.

If the-s parameter is used, two code is generated in the directory, one for the. Class code. A copy of the. Java code.

. Class code, which can be used after packaging: Java code can be copied directly to our project to run.

A tentative study of webservice (simple small demo)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.