Introduction to WEB Service research
? Simply put, WebService is a Web service that is a remote invocation technique across programming languages and operating systems. The transport of WebService relies on the HTTP protocol for data transmission using the SOAP protocol in XML format.
? The three elements of WebService are as follows:
- Soap (Simple Object access Protocol): An easy-to-reach protocol that describes the format of passing information by soap
- WSDL (webservices Description Language): A Web Service Description language that describes how to access specific interfaces, in most cases automatically generated by software
- UDDI (Universal Description Discovery and Integration): General description, Discovery, and integration for managing, distributing, querying WebService
Web Service Instance
? The following gives a specific example of webservice to illustrate the webservice development of the original ecology
Server-side development WebService interface and implementation class code writing
? The specific code is as follows:
@WebServicepublicinterface HelloService { @WebMethod sayHello(String name);}@WebService // 实现类必须要添加@WebService注解publicclassimplements HelloService { @Override publicsayHello(String name) { return"你好:" + name; }}
Service Release Code authoring
? Above we define the interfaces that need to be published, and the next is to publish the interface to the registry to make it a WebService service. The specific code is as follows:
publicclass WebServicePublisher { publicstaticvoidmain(String[] args) { // STEP1: 定义服务发布的地址 "http://localhost:9999/WS/HelloService"; // STEP2: 发布WebService服务 Endpoint.publishnewHelloServiceImpl()); // 第一个参数表示发布到注册中心的地址 第二个参数表示当有客户端向该地址发送请求的时候,服务器端对应的处理类 // STEP3: 打印发布服务成功信息 System.out.println("HelloService发布成功!"); }}
Accessing a WSDL file via a URL
? After the service has been published successfully, we can get the WSDL file of the registry directly from the browser input: http://localhost:9999/WS/HelloService?wsdl:
<definitionsxmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:tns="http://server.webservice.rampage.com/"xmlns:xsd="Http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/"targetnamespace="http://server.webservice.rampage.com/"name="Helloserviceimplservice"><types><xsd:schema><xsd:importnamespace="http://server.webservice.rampage.com/"schemalocation="Http://localhost:9999/WS/HelloService?xsd=1"/></xsd:schema></types><messagename="SayHello"><partname="Parameters"element="Tns:sayhello"/></message><messagename="Sayhelloresponse"><partname="Parameters"element="Tns:sayhelloresponse"/></message><porttypename="Helloserviceimpl"><operationname="SayHello"><inputmessage="Tns:sayhello"/><outputmessage="Tns:sayhelloresponse"/></operation></portType><bindingname="Helloserviceimplportbinding"type="Tns:helloserviceimpl"><soap:bindingtransport="Http://schemas.xmlsoap.org/soap/http"style="Document"/><operationname="SayHello"><soap:operationsoapaction=""/><input><soap:bodyuse="literal"/></input><output><soap:bodyuse="literal"/></output></operation></binding><servicename="Helloserviceimplservice"><portname="Helloserviceimplport"binding="Tns:helloserviceimplportbinding"><soap:addresslocation="Http://localhost:9999/WS/HelloService"/></port></service></definitions>
? A description of the WSDL document is as follows:
- Types : A container for a data type definition that uses a type system (typically using a type system in XML schemas). (data type of the incoming and outbound parameters)
- message: Abstract typed definition of the data structure of the communication message. Use the type defined by types to define the data structure of the entire message (entry and exit parameters)
- Operation: An abstract description of the operations supported in the service, typically a single operation describes a request/Response message pair (method) for an Access portal
- PortType: An abstract collection of operations supported by an access entry point type that can be supported by one or more service access points (service class)
- Binding: A specific service access point is bound to a specific service class (without looking at the content, looking at the relationship)
- Port: defined as WebService single service access point
- Service: Collection of related service access points
Client-side development automatically generates client code
? There are two ways we can generate client code
? 1. If the server is already started, we can use the Wsimport command in CMD to generate the code automatically:
Wsimport-s E:\workspacce\myWebServiceClient\src-keep http://localhost:9999/WS/HelloService?wsdl
? Where-s parameter specifies the generated source path, here I specify the client code path (do not need to specify the specific package path, generated code will be automatically generated to the server side of the same path folder). The resulting file structure is as follows:
? At this point we write the client test code:
publicstaticvoidmain(String[] args) { // STEP1: 实例化WebService工厂 newHelloServiceImplService(); // STEP2: 得到远程的Service实例 HelloServiceImpl serviceImpl = serviceFactory.getHelloServiceImplPort(); // STEP3: 远程调用WebService方法 System.out.println(serviceImpl.sayHello("KiDe")); }
- If the server does not start, we only have the WSDL file, we can create a new Web service client project in Eclipse, and select the WSDL file, and then all the way next. The resulting code structure is:
? The client test code we wrote is:
Public classWebserviceinvoker { Public Static void Main(string[] args)throwsServiceexception, RemoteException {/*** METHOD1: Get remote interface via Helloserviceimplservicelocator */ /*//STEP1: Instantiating WebService factoryHelloserviceimplservice servicefactory = new Helloserviceimplservicelocator (); //STEP2: Get a remote service instanceHelloserviceimpl Serviceimpl = Servicefactory.gethelloserviceimplport (); ///STEP3: Remote Call to WebService methodSystem.out.println (Serviceimpl.sayhello ("Kide")); * * /*** METHOD2: Get remote interface via proxy class */ //STEP1: Instantiate WebService factoryHelloserviceimplproxy Serviceproxy =New Helloserviceimplproxy();//STEP2: Get a remote service instanceHelloserviceimpl Serviceimpl = serviceproxy.Gethelloserviceimpl();///STEP3: Remote Call to WebService methodSystem. out.println(Serviceimpl.SayHello("Kide"));//System.out.println (Serviceproxy.sayhello ("Kide"));//You can also invoke the service directly from the proxy class}}
? Similarly, we can automatically generate server-side code by specifying the way the remote interface implements the class. In fact, the way to create a client or server through eclipse is to use the CXF framework. You will find that the WSDL generated test class calls the remote WebService service rather slowly, and the following warning appears when running:
classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
? Online search found that because of the lack of Activation.jar and Mail.jar, the two jars added into the project's build path after the discovery of alarm elimination, but the execution is very long. This shows that the CXF framework simplifies development, but the efficiency of the implementation is greatly compromised.
Using CXF for WebService development
? The encyclopedia's explanations on CXF are as follows:
Apache CXF is open source, and CXF is a combination of two projects: Celtix developed by Iona Technology Inc. (now part of progress) and Codehaus, developed by Xfire-hosted team, which was jointly completed by people at the Apache Software Foundation. CXF's name comes from the initials of "Celtix" and "XFire".
? The official web site is as follows:
Apache CXF? is an open Source services framework. CXF helps you build and develop services using frontend programming APIs, like Jax-WS and Jax-rs. These services can speak a variety of protocols such as SOAP, Xml/http, RESTful HTTP, or CORBA and work over a variety of Transports such as HTTP, JMS or JBI.
Download Apache CXF
? Go directly to the official website http://cxf.apache.org/download, here i download the version is: 3.2.1. After extracting the file directory structure as follows:
? The specific catalogue is described below:
- Licenses: Related license agreement for referenced third-party jar packages
- Modules: Contains binary package files that are compiled separately by the CXF framework based on different characteristics. When you publish a WEB project that is based on the CXF framework, you can choose to use all the. jar files in that directory, or you can select the Cxf-2.0.2-incubator.jar file in the Lib directory
- Samples: Contains an example of all the CXF binary package releases, including the source code for these samples and the associated Web application configuration file, which makes it easy to compile and run tests with Ant to understand how the CXF is developed and used. You can learn more about the compile and run steps of the sample through the Samples directory and the README.txt files in its various subdirectories
- LIB: Development of related jar packages that may be needed based on the CXF framework
- Related toolkit provided by the BIN:CXF framework
Development WebService server-side introduction of related jar packages
? Create a new Java project directly, and then add all the jar packages under the cxf Lib directory to the project's build path.
Write the service interface and publish
? Directly on the code:
@WebService@BindingType(Value=javax.XML.ws.Soap.soapbinding.soap12http_binding) Public InterfaceHelloService {StringSayHello(String name);}@WebService Public classHelloserviceimplImplementsHelloService {@Override PublicStringSayHello(String name) {return "Hello:"+ Name; }} Public classServicepublisher { Public Static void Main(string[] args) {//STEP1: Create a bean factoryJaxwsserverfactorybean Factorybean =New Jaxwsserverfactorybean();///STEP2: Set the interface to which the service publishing address has been publishedFactorybean.setaddress("Http://localhost:8888/ws/HelloService"); Factorybean.Setserviceclass(HelloService.class); Factorybean.Setservicebean(New Helloserviceimpl());//STEP3: Publish serviceFactorybean.Create(); System. out.println("HelloService service released successfully!"); }}
? Note that the JDK1.8 is needed here .
Developing WebService clients using CXF tools to generate client code
? Execute the following command under the Bin directory of the CXF tool:
Wsdl2java–d. http://localhost:8888/ws/HelloService?wsdl
? The corresponding client code is generated in the bin directory, and the code is consistent with the direct wsimport
Writing client-side test code
? Consistent with the previous, here no longer explains
?
WebService Call Flow Analysis
? Implementing a complete WebService service consists of the following steps:
The Web service provider designs the implementation Web service and publishes the correct Web service through the Web Service Broker and Registers (publishes) in the UDDI registry
A Web service requester requests a specific service from a Web Service Broker, and the mediator queries the UDDI registry on request to find the service that satisfies the request (discovery)
Web Service Broker Returns a Web service description information that satisfies a condition to a Web service requester, which is written in WSDL and can be read (discovered) by various machines that support Web services
Generate the corresponding SOAP message from the description information returned by the Web service mediator and send it to the Web service provider to implement the invocation (binding) of the Web service
The Web service provider executes the corresponding Web service by SOAP message and returns the result of the service to the Web service requestor (binding)
Using WebService in a web App
Adding @webservice annotations on the service interface
- Creating a service Interface implementation class
Add cxf servlet configuration in Web. xml
xml <servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>
- Configure Cxf-servlet.xml
xml <jaxws:server id="bye" address="/bye" serviceClass="com.rl.cxf.web.inter.ByeInter"> <jaxws:serviceBean> <bean class="com.rl.cxf.web.inter.ByeInterImpl"></bean> </jaxws:serviceBean> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:outInterceptors> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:inInterceptors> </jaxws:server>
- Using Wsdl2java to create client code based on WSDL address
- Write a client use case to make a call
Other WebService related jar packages introduced via POM
? In general, if a Web project wants to integrate the CXF framework for WebService calls, at least CXF related JAXWS and HTTP protocol support should be introduced
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.1</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.1</version></dependency>
Reference links
http://blog.csdn.net/jiandanfeng2/article/details/53439748
http://blog.csdn.net/qq_14852397/article/details/52761425
http://blog.csdn.net/aqsunkai/article/details/51711087
http://blog.csdn.net/qq32933432/article/details/51394749
http://blog.csdn.net/qq_35712358/article/details/71244342
Https://www.cnblogs.com/fengwenzhee/p/6915606.html
http://blog.csdn.net/yangwenxue_admin/article/details/51059125
? integration with the spring framework see the remote calls and webservice of the spring Official document interpretation for subsequent updates
?
A probe into Web service