Talk about WebService development-Basic article __webservice

Source: Internet
Author: User
Tags soap webservice annotation wsdl

Recently, some friends on the Internet asked about the development of webservice, here on my personal understanding to do a concise introduction and summary. This article mainly introduces some basic webservice involved: the necessity of WebService; Schema specification and HTTP protocol
Important discourse; The steps of developing webservice;


the necessity of WebService

For this answer to the following two questions, the individual feels the need to understand it:

1. What is WebService;

As the name suggests, WebService is a web-based service that provides some resources for application access, a cross-language, cross-platform specification, and a solution for communication between multiple cross-platform and cross-language applications;

2. Why to use WebService;

Imagine the following scenario a application---Java development, running on the Windows platform,list<address> getalladdress ();

b Application---in C language development, running on the Linux platform, b application now to get a application of all the address information;

Summary: WebService can be resolved across languages , Cross-platform, remote calls .

Schema specification and HTTP protocol

Schema specification is used to constrain the XML file's book-type, schema file is suffix with. xsd, the schema specification needs to have the following points: All tags and attributes need to be defined in the schema file, the schema tag itself is defined by the organization of the Consortium;  Each schema file must have an ID, which is called the namespace namespace, and targetnamespace identifies the namespace of the current schema;  The value of the schema,xmlns referenced by xmlns is the namespace of the referenced schema, which requires at least N-1 individual names when referencing n schemas, and the format of the alias is "XMLNS:DDD"; For schemas that are not defined by the schemalocation organization, you need to specify the location of the referenced schema, which is the location information that describes the referenced schema.


HTTP protocol, you need to understand the following: the composition of the request, a request line [request method (Post/get), Path, protocol version (http1.1)], multiple request headers, the request body (only post method has);
The composition of the response, the response status line [need to understand the commonly used status code 200,404,500,302, etc.]; request the response process, understand the difference between http1.0 and http1.1, 1.0 a connection can only request a resource, now basically not, the specific process as follows:


Important Discourse Wsdl:web Service definition Language corresponds to a suffix of. wsdl file, a webservice corresponding to a unique WSDL document, which defines the data format of the request and response that the client interacts with the server side; Soap:simple Object Access protocal simple Image Access Protocol, which is actually http+xml fragment; dependent on the definition of the WSDL document; Sei:service EndPoint Interface web Service Terminal interface, is the server to handle the request of the interface (which is the method of processing the request);


The steps to develop WS can be developed using the source-born JDK. Recommended more than 1.6 jdk; Of course, the work may be the use of other WebService framework for development, commonly used CXF,AXIS2, etc. here, start with the source of the JDK to develop a simple demo to show steps: 1. Create the server-side engineering Webservice-jdk-server and create a new interface Isayhello.java, that is, the SEI is created.

Package com.devins.ws.server.service;

Import Javax.jws.WebMethod;
Import Javax.jws.WebService;

/**
 * * 
 sei definition
 */
 @WebService public
interface Isayhello {
	@WebMethod
	public String SayHello (string name);


2. New Interface Implementation class:
Package Com.devins.ws.server.service.impl;

Import Javax.jws.WebService;

Import Com.devins.ws.server.service.ISayHello;

/**
 * SEI implementation
 /
@WebService public
class Sayhelloimpl implements isayhello{

	@Override
	Public String SayHello (string name) {
		//TODO auto-generated method stub
		System.out.println ("server:hello!" +name);
		Return "Hello:" +name
	}


The above two-step attention points to @webservice annotation sei and SEI implementation class, to @webmethod annotation sei method;
3. Publishing Service:
Package com.devins.ws.server.publish;

Import Javax.xml.ws.Endpoint;

Import Com.devins.ws.server.service.impl.SayHelloImpl;

public class Servicepublishtest {public
	
	static void Main (string[] args) {
		String address = ' http://132.122.239.7 4:8089/ws/sayhello ";
		Use the Endpoint class to publish
		Endpoint.publish (address, New Sayhelloimpl ());
		SYSTEM.OUT.PRINTLN ("WebService service announcement was successful.) ");
	}

}

The server engineering structure is as follows:

Complete the above encoding and run the main method of the publishing service, which can be entered in the browser
Http://132.122.239.74:8089/ws/sayhello?wsdls View the WSDL information to indicate that the service was published successfully. The following figure:


4. Create the client project, through the JDK tool Wsimport.exe to generate the client call code, through the command line into the client project SRC directory, the command is as follows:

Wsimport-keep URL here is the URL of the server-side of the WSDL address, executed as follows:


After the execution is completed, the client project is refreshed and the project generates the following code:



5. Write the client calling code:

Package com.devins.ws.client;

Import Com.devins.ws.server.service.impl.SayHelloImpl;
Import Com.devins.ws.server.service.impl.SayHelloImplService;
 * * * * the way to generate client-side code via a remote WSDL address * * Public
class Clienttest {public
	static void Main (string[] args) {
		Sayhelloimplservice factory = new Sayhelloimplservice ();
		Sayhelloimpl Sayhelloimpl = Factory.getsayhelloimplport ();
		
		String result = Sayhelloimpl.sayhello ("Devins");
		SYSTEM.OUT.PRINTLN ("Client:" +result);
	}

So far, the development of webservice through the JDK has been completed, the right key to run the client code results are as follows:

Client console:


Server-side consoles:



By implementing the client in the above way, we find that for the development of the coding implementation phase, it is not convenient to view the SOAP message, that is, request message and response message, Eclipse has helped our developers think that Eclipse provides a tool to monitor TCP/IP to perform the following configuration steps:
1. Generate a local WSDL document, you can run Http://132.122.239.74:8089/ws/sayhello?wsdls browser to save related content into WSDL file, such as SERVICE.WSDL;
2. Modify the local SERVICE.WSDL publishing port is 8080, you can set other free ports as required;

<service name= "Sayhelloimplservice" >
		<port name= "Sayhelloimplport" TNS: Sayhelloimplportbinding ">
			<soap:address location=" Http://132.122.239.74:<span style= "color: #ff0000;" >8080</span>/ws/sayhello "/>
		</port>
	</service>


3. Generate client code based on local WSDL document, here is another client project Webservice-jdk-client2, the command line into the SRC directory method is as follows:


The other code is the same as the first client project.

4. Configure eclipse TCP/IP listening and configure the following figure:



After completing these steps, you can right-click on the client-side call code, and the following TCP/IP listening window appears in the console, as shown below:



From the above figure can be more convenient to see the request message and response message to facilitate the development process of viewing and debugging. At this point, the process of using JDK to develop WebService has been completed. The follow-up will also be a step-by-step introduction to knowledge about WebService.



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.