WebService Instance & Invoke Nature & Privilege Control

Source: Internet
Author: User
Tags soap wsdl

Here is a first example of developing WebService with CXF. Then it introduces the invocation nature and permission control of WebService.


First, the example
1.1 Downloads Apache-cxf

The address is as follows: http://cxf.apache.org/download.html, to download the stable version.

  

  


1.2 Developing WebService server with CXF

Each WebService component requires 2 parts, an interface, and an implementation class. The development of a webservice component typically requires three steps:

? Develop a WebService service interface that is decorated with @webservice.

? To develop a WebService implementation class, the implementation class also needs to be decorated with @webservice.

? Use the static method of the endpoint class to publish the WebService.

The steps are as follows:

First, create a new javaproject:01_ws_server, which is the server for WebService, and import the required jar packages:


  


Developing the Business Interface interface HelloWorld:

Package Com.tgb.cxf.ws.interfaces;import javax.jws.webservice;/** * Business interface, to be decorated with @webservice *  * @author Wangzhipeng * *  /@WebServicepublic interface HelloWorld {public void Sayhi (String name);}

Development Business Implementation class HELLOWORLDWS implements HelloWorld:

Package Com.tgb.cxf.ws;import Javax.jws.webservice;import com.tgb.cxf.ws.interfaces.helloworld;/** * Business implementation, Also use @webservice to decorate *  * @author Wangzhipeng * * *  /@WebService (Endpointinterface = " Com.tgb.cxf.ws.interfaces.HelloWorld ", ServiceName =" Helloworldws ") public class Helloworldws implements HelloWorld {@ overridepublic void Sayhi (String name) {System.out.println ("Hello" + Name);}}

Use the static method of the endpoint class to publish the WebService:

Package Com.tgb.cxf.ws.publishes;import Javax.xml.ws.endpoint;import Com.tgb.cxf.ws.helloworldws;import com.tgb.cxf.ws.interfaces.helloworld;/** * Use static methods of the endpoint class to publish WebService *  * @author Wangzhipeng *  */public Class Servermain {public static void main (string[] args) {HelloWorld HelloWorld = new Helloworldws ();//Call Endpoint Publish method to publish the Web Serviceendpoint.publish ("Http://192.168.24.144/helloworld", HelloWorld); SYSTEM.OUT.PRINTLN ("Web service published successfully!! ");}}

To see if the WebService was published successfully (the following page appears to indicate a successful publication):


  


1.3 Developing WebService clients with CXF

Developing webservice clients typically requires the following steps:

First we create a new javaproject:01_ws_client, as a client of WebService (the new WSClient class is used to invoke the server):


  


Call the Wsdl2java tool provided by CXF (such as) to generate the appropriate Java code based on the WSDL document (the published address is successful). WSDL, which is the Web Service definition language. Any language that implements the WebService needs to provide and expose the WSDL document.

The Apache-cxf Bin directory is first added to the environment variable path, which facilitates the use of the Wsdl2java tool later.


  


Then open the cmd Command window and go to the SRC directory of the 01_ws_client project, such as:


  


Then, execute the command wsdl2javahttp://192.168.24.144/helloworld?wsdl (the access address for the server we posted)


  


In this way, you can generate the server-side code into the client project, and refresh our client project to see:


  


Then, write the following code in our newly created WSClient class to invoke the WebService server:

Package Com.tgb.ws.client;import Com.tgb.cxf.ws.helloworldws;import Com.tgb.cxf.ws.interfaces.helloworld;public Class WSClient {public static void main (string[] args) {//Helloworldws for the generated com.tgb.cxf.ws package inherits the service classes HELLOWORLDWS Factory = new Helloworldws ();//This returns only the remote WebService "proxy" HelloWorld HelloWorld = Factory.gethelloworldwsport (); Helloworld.sayhi ("Wangzhipeng");//output: Hello Wangzhipeng, which means call WebService succeeded}}

Here we need to find the Wsdl2java generated by the service-side class, a class that inherits the service, which can be used as a factory, with the Getxxxport method of the class to return the proxy of WebService. This is the end of the instance.


second, the essential principle of calling WebService

A webservice call is actually not a method call, but rather a SOAP message, an XML document fragment. Call the detailed procedure as follows:

1. The client will invoke the method, parameter, and transform to generate the XML document fragment (SOAP message, input message), which must conform to the WSDL-defined format.

2. The client passes the generated XML document fragment to the server over the network.

3. The server accepts fragments of XML documents sent to the client.

4. The server parses the XML document fragment, extracts the data from it, and converts the data to the parameter values required to invoke webservice.

5, the server execution method.

6. The server converts the execution result of the method to an XML document fragment (SOAP message, output message), which must conform to the WSDL-defined format.

7. The server sends an XML document fragment of the execution result over the network to the client through the network.

8. The client receives an XML document fragment of the execution result.

9. The client parses the XML document fragment that executes the result, extracts the data from it, and converts the data to the return value of the call WebService.

From the above call essentially, the execution of the method is on the server side, the client only sends the XML, receives the XML, parses the XML. Therefore, the only requirement of a language support webservice is that the language supports parsing, generation, and network transmission of XML documents. Why is webservice inseparable from XML? The three foundations of WebService are as follows:

1. wsdl:web Service definition Language--webservice Definition language

2. Soap:simple Object access protocol--Simple Object Accessing protocol

3, Uddi:universal Description Discovery and integration--General description, Discovery and Integration Services, is a directory service

Both WSDL and SOAP are XML, so WebService is inseparable from XML.


iii. Types of CXF

The WebService method of the publication, when the formal parameter, the return value type is String, the base data type, CXF can certainly easily handle, for example: public void Sayhi (String name);

When the formal parameter, the return value type is the JavaBean type compound class, the List collection, the array and so on, CXF also can handle very well, for example: public list<cat> querycatsbyuser (user user);

There are also some types of composite classes like MAP, non-JavaBean, cxf are not able to handle, for example: public map<string, cat> getallcats () The exception is reported at this time: javax.xml.ws.WebServiceException:org.apache.cxf.service.factory.ServiceConstructionException.

For a type that cannot be processed, for example: map<string, cat> needs us to manually write the tool class to convert it to a type that CXF can handle, for example, we can convert it to the following types:

Package Com.tgb.cxf.ws.utils;import Java.util.list;import com.tgb.cxf.domain.cat;/** * will CXF cannot handle type map<string, Cat > Convert to the following CXF can handle type Stringcat * @author Wangzhipeng * */public class Stringcat {public static class Entry {private String K Ey;private cat Value;public Entry () {};p ublic Entry (String key, Cat cat) {This.key = Key;this.value = Cat;} Public String GetKey () {return key;} public void Setkey (String key) {This.key = key;} Public Cat GetValue () {return value;} public void SetValue (Cat value) {this.value = value;}} Private list<entry> entries;public list<entry> getentries () {return entries;} public void Setentries (list<entry> entries) {this.entries = entries;}}

The conversion classes are as follows:

Package Com.tgb.cxf.ws.utils;import Java.util.hashmap;import Java.util.list;import java.util.map;import Javax.xml.bind.annotation.adapters.xmladapter;import Com.tgb.cxf.domain.cat;import com.tgb.cxf.ws.utils.stringcat.entry;/** * CXF type conversion class map<string, Cat>--stringcat *  * @author Wangzhipeng *  */public class Fkxmladapter extends Xmladapter<stringcat, map<string, cat>> {@Overridepublic Stringcat Marshal (map<string, Cat> v) throws Exception {Stringcat tstringcat = new Stringcat (); for (String KEY:V.K Eyset ()) {tstringcat.getentries (). Add (New Entry (Key, V.get (key));} return tstringcat;} @Overridepublic map<string, cat> unmarshal (Stringcat v) throws Exception {map<string, cat> catmap = new Hashma P<string, cat> (); list<entry> entrylists = V.getentries ();//  for (Entry entry:entrylists) {catmap.put (Entry.getkey (), Entry.getvalue ());} return catmap;}}

Then we need to add @xmljavatypeadapter (conversion Class) to the corresponding method of the WebService interface, and the others will remain the same:


  

Package Com.tgb.cxf.ws.interfaces;import Java.util.list;import Java.util.map;import javax.jws.webservice;import Javax.xml.bind.annotation.adapters.xmljavatypeadapter;import Com.tgb.cxf.domain.cat;import Com.tgb.cxf.domain.user;import com.tgb.cxf.ws.utils.FkXmlAdapter; @WebServicepublic interface HelloWorld {public void Sayhi (String name);p ublic list<cat> querycatsbyuser (user user);//CXF cannot handle map<string,cat> type, So we use Fkxmladapter to deal with public @XmlJavaTypeAdapter (Fkxmladapter.class) map<string, cat> getallcats ();}


iv. Control of permissions

The WebService service, as long as we have access to the address, can be called arbitrarily, which is obviously inappropriate. So we also need to verify the permissions. The idea of WebService permission control can be as follows:

The server requires that the input message always carry a user name, password information, and if the information is not carried, or the information is incorrect, then the call is denied directly. We can use interceptors provided by CXF to achieve this effect. is for programmers to be able to access and modify the SOAP messages generated by the CXF framework, CXF provides interceptors. We can add the out interceptor on the client, intercept the SOAP message, add the user name, password information, and then send to the server, then we add in interceptor on the server to intercept the SOAP message, and then verify the user name and password information;


  

  


Interception structures such as:


  


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WebService Instance & Invoke Nature & Privilege Control

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.