Getting started with Web Service

Source: Internet
Author: User
Tags representational state transfer

1. Web Service Definition
Currently, there is no uniform definition of Web Service. Definition 1: Web Service is a self-contained, modular application that can be described, published, searched, and called on the Web. Definition 2: A Web Service is a network-based, distributed modular component that executes specific tasks and complies with specific technical specifications, these specifications enable the Web Service to operate with other concurrently-used components. Definition 3: Web services are online application services released by enterprises to meet their special business needs. Other companies or application software access and use this application Service over the Internet. I prefer Wikipedia's definition: Web Service is a Service-oriented architecture technology that provides services through standard Web protocols to ensure the interoperability of application services on different platforms.
Currently, WebService is not fully and accurately defined, but generally Web services include:
SOAP
An XML-based scalable message envelope format must be bound to a transport protocol at the same time. This Protocol is usually HTTP or HTTPS, SMTP, XMPP.
WSDL
An XML document used to describe the details of the Service port access method and protocol. It is usually used to generate server and client code and configuration information. For example, wsdl2java tool in JAVA.
UDDI
A protocol used to publish and search WEB Services. Applications can use this Protocol to find the target WEB Service at design or runtime.
 
2. Web Service
Remote process call
Web Service provides a distributed function or method interface for users to call. This is a traditional method. Generally, the RPC interface is defined in WSDL. Despite the wide use of RPC in the initial deployment of Web Services, the sound of Close coupling is also endless. The reason is that the RPC Web Service uses a simple ing to directly convert user requests to functions or methods written in a specific language.
Service-Oriented Architecture
Compared with RPC-based Web Services, SOA has been supported and affirmed by most major software vendors and industry experts. Because the SOA method pays more attention to how to connect to the service rather than the specific implementation details.
Restful services
The Representational state transfer (REST) is similar to the HTTP protocol. REST limits interfaces to a set of HTTP operations, such as the pair, this service can describe the content of a SOAP message through WSDL, limit the action interface through HTTP, or abstract the action completely in SOAP.
 
3. HelloWorld
I will use the Apache Cxf official website's How To example To implement helloworld and subsequent examples, and I will use maven tool To build the example project. For cxf and maven, please refer to the official website http://cxf.apache.org, http://maven.apache.org. (Example: Use maven to build the project pom. xml)
Compile Web Service
HelloWorld. java
 
,,
@ WebService
Public interface HelloWorld {
String sayHi (@ WebParam (name = "text") String text); // The text here is the input parameter
}
HelloWorldImpl. java

Package demo. hw. server;
Import java. util. LinkedHashMap;
Import java. util. Map;
Import javax. jws. WebService;
@ WebService (endpointInterface = "demo. hw. server. HelloWorld", serviceName = "HelloWorld ")
Public class HelloWorldImpl implements HelloWorld {
Public String sayHi (String text ){
System. out. println ("sayHi called ");
Return "Hello" + text;
}
}
Publish Service
Server. java
 
System. out. println ("Starting Server ");
HelloWorldImpl implementor = new HelloWorldImpl ();
String address = "http: // localhost: 9000/helloWorld"; // publish address
Endpoint. publish (address, implementor );
Access Web Service
Client. java
 
 
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean ();
Factory. getInInterceptors (). add (new LoggingInInterceptor ());
Factory. getOutInterceptors (). add (new LoggingOutInterceptor ());
Factory. setServiceClass (HelloWorld. class );
Factory. setAddress ("http: // localhost: 9000/helloWorld"); // The address is the published address.
HelloWorld client = (HelloWorld) factory. create ();
String reply = client. sayHi ("HI ");
System. out. println ("Server said:" + reply );
System. exit (0 );
3. XML and JSON
Both are Web Service resource representations. To put it bluntly, Web Service requests or returns results using either of them. They are defined as follows:
1. JSON is a lightweight data exchange format with good readability and ease of writing. In addition, JSON uses a highly compatible text format.
2. XML extension markup language, used to mark electronic files so that they have a structured markup language. It can be used to mark data and define data types, is a source language that allows you to define your own markup language. XML is very suitable for Web transmission. It provides a unified method to describe and exchange structured data independent of applications or vendors.
 
Comparison of XML and JSON advantages and disadvantages:
1. The two types of readability are comparable. JSON format {property1: value1, property2: value2 ,...}, The XML format is value1value2. In terms of description, XML is better than JSON.
2. Scalability: XML can be used, and JSON can also be used.
3. Data size. JSON is superior to XML, but the difference is not big.
4. Currently, XML is widely used in the industry, while javascript supports JSON better than XML.
5. The performance has been tested, And the JSON parsing speed is almost 10 times faster than that of XML parsing.
 
4. Integration of CXF and Spring framework
Assume that Maven is used to build the project, pom. xml
Compile Web Service
HelloWorld. java
 
Package demo. spring. service;
Import javax. jws. WebService;
@ WebService public interface HelloWorld {
String sayHi (String text );
}
Interface implementation class HelloWorldImpl. java
 
Package demo. spring. service;
Import javax. jws. WebService;
@ WebService (endpointInterface = "demo. spring. service. HelloWorld ")
Public class HelloWorldImpl implements HelloWorld {
Public String sayHi (String text ){
System. out. println ("sayHi called ");
Return "Hello" + text;
}
}
Create a new WEB-INF file in the project's cxf-servlet.xml directory with the following content:
You also need to set the Servlet. Generally, there is a Web. xml file in the Web project. We need to add the Spring ContextLoaderLister element. When the Web container is started, Spring ContextLoaderLister starts Srping and loads the configuration file. These configuration files are defined by the context-param element.
...
 
ContextConfigLocation WEB-INF/beans. xml // configuration file
Org. springframework. web. context. ContextLoaderListener
Finally, create a client in the preceding beans. xml file.
Here you can choose to inject "helloClient" into the Srping bean, or manually find it from Spring application context:
 
ApplicationContext context = ...; // Your Spring ApplicationContext
HellWorld client = (HelloWorld) context. getBean ("helloClient ");
 

Related Article

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.