WebService with Notes

Source: Internet
Author: User
Tags soap wsdl

WebService: Cross-platform, system, inter-language calls to each other


Cxf:
Axis (Apache)-Axis2 (Apache)
XFire-CXF (Celtrix + XFire) (Apache)


Xfire--webservice Frame
CELTRIX--ESB Framework, Enterprise service bus, ie


SOA (service-oriented architecture)
Service1, Service2, Service3 ... All components are "plug and Play" and each component provides a service


IBM advocates SOA-oriented architecture and wants to develop software in a "computer-assembled" way


1. Components for various services (WebService)
2. Enterprise Service Bus (ESB)


CXF claims to be the SOA framework
CXF built-in one jetty Web server, better than Tomcat


-----------------------------------------------------------------------------
Using CXF to develop WebService server:
/************* Each WebService component requires 2 parts, interface and implementation class *************/
1. Develop a WebService business interface
The interface must be decorated with @webservice
2. Develop a WebService implementation class
The implementation class is also @webservice decorated with @WebService (endpointinterface= "Org.cxf.ws.HelloWorld", Servicename= "Helloworldws")


The final Web page is generated as follows:
-----------------------------------------------------------------------------------------------------------
This XML file does does appear to has any style information associated with it. The document tree is shown below.
<!--
Published by Jax-ws RI at http://jax-ws.dev.java.net. Ri ' s version is JAX-ws RI 2.2.4-B01.
-
<!--
Generated by Jax-ws RI at http://jax-ws.dev.java.net. Ri ' s version is JAX-ws RI 2.2.4-B01.
-
<definitions xmlns:wsu= "Http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp= "Http://www.w3.org/ns/ws-policy" xmlns:wsp1_2= "Http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns: Wsam= "Http://www.w3.org/2007/05/addressing/metadata" xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/" xmlns: tns= "http://impl.ws.cxf.org/" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" xmlns= "http://schemas.xmlsoap.org/ wsdl/"targetnamespace=" http://impl.ws.cxf.org/"name=" Helloworldws ">
<import namespace= "http://ws.cxf.org/" location= "http://127.0.0.1/helloWorldWs?wsdl=1"/>
<binding xmlns:ns1= "http://ws.cxf.org/" name= "helloworldwsportbinding" type= "Ns1:helloworld" >
<soap:binding transport= "http://schemas.xmlsoap.org/soap/http" style= "Document"/>
<operation name= "Sayhi" >
<soap:operation soapaction= ""/>
<input>
<soap:body use= "literal"/>
</input>
<output>
<soap:body use= "literal"/>
</output>
</operation>
</binding>
<service name= "Helloworldws" >
<port name= "Helloworldwsport" binding= "tns:helloworldwsportbinding" >
<soap:address location= "Http://127.0.0.1/helloWorldWs"/>
</port>
</service>
</definitions>
-------------------------------------------------------------------------------------------------------------
3, Release WebService
Use static methods of the endpoint class to publish Web Service


-----------------------------------------------------------------------------------
Developing Web Clients using CXF
1. Call the Wsdl2java tool provided by CXF to generate the corresponding Java code from the WSDL document--CMD using the command: WSDL http://127.0.0.1/helloWorldWs?wsdl
WSDL = Web Service Definition Language The Web Services Description Language


Web Service is implemented in any language and requires exposing a WSDL document


2. Find the class that Wsdl2java generated, a class that inherits the service interface
Instances of this class can be used as a factory


3. Call the Getxxxport method of the service subclass instance to return a proxy for a remote Web service


Formal parameters, return values
1, when the type of formal parameter, return value is a string basic data type, CXF can easily handle
2, when the formal parameter, the return value type is the JavaBean type collection class, the list collection, the array and so on, CXF can handle?
CXF can also handle it very well,
3, there are some like map, non-javabean type of composite class, CXF is not able to handle


Three technical Fundamentals of WEB service:
1. WSDL Web Service Definition Language


--------------------------------------------------------------------------
There are two important attributes in XML:
TargetNamespace, equivalent to the package in Java
xmlns namespace, equivalent to import in Java


WSDL Document:
1. Web Service Interface
<types>...</types>
<message name= "Sayhi" >...</message>
<message name= "Sayhiresponse" >...</message>
<message name= "Getcatsbyuser" >...</message>
<message name= "Getcatsbyuserresponse" >...</message>
<porttype name= "HelloWorld" >...</portType>
The types element, which is the schema document
2N message Element (n is the number of methods under the service end)
porttype element, containing n operation child elements (each operation defines a WS operation--method)


(mainoccurs= "0", indicating the occurrence of 0 to 1 times, maxoccurs= "unbounded", indicating undecided, countless times)


2. Web Service Implementation
A binding element containing 2N operation elements
service element, containing port child element--Specifies the address of the specified Web service binding


--------------------------------------------------------------------------


2. SOAP Simple Object Access Protocol
3. UDDI


---------------------------------------------------------------------------
<xs:complextype name= "Sayhi" >
<xs:sequence>
<xs:element name= "arg0" type= "xs:string" minoccurs= "0"/>
</xs:sequence>
</xs:complexType>
<xs:complextype name= "Sayhiresponse" >
<xs:sequence>
<xs:element name= "return" type= "xs:string" minoccurs= "0"/>
</xs:sequence>
</xs:complexType>
<xs:complextype name= "Getcatsbyuser" >
<xs:sequence>
<xs:element name= "arg0" type= "Tns:user" minoccurs= "0"/>
</xs:sequence>
</xs:complexType>
<xs:complextype name= "User" >
<xs:sequence>
<xs:element name= "Address" type= "xs:string" minoccurs= "0"/>
<xs:element name= "id" type= "xs:int" minoccurs= "0"/>
<xs:element name= "name" type= "xs:string" minoccurs= "0"/>
<xs:element name= "Pass" type= "xs:string" minoccurs= "0"/>
</xs:sequence>
</xs:complexType>
<xs:complextype name= "Getcatsbyuserresponse" >
<xs:sequence>
<xs:element name= "return" type= "Tns:cat" minoccurs= "0" maxoccurs= "unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complextype name= "Cat" >
<xs:sequence>
<xs:element name= "Color" type= "xs:string" minoccurs= "0"/>
<xs:element name= "id" type= "xs:int" minoccurs= "0"/>
<xs:element name= "name" type= "xs:string" minoccurs= "0"/>
</xs:sequence>
</xs:complexType>
---------------------------------------------------------------------------------------
A Web service call--not actually a method call, but a SOAP message (that is, an XML document fragment), the nature of the CXF call:
1. The client converts the parameters that need to be called into an XML document fragment (SOAP message, input message)
--The fragment must conform to the WSDL-defined format
2, the client through the network, the XML document fragments to the remote server
3. The server receives an XML document fragment
4, the server parses the XML fragment, extracts the data, and transforms the data into the parameters or objects required to invoke the Web service
5, the service Side Execution method
6, the service side of the execution method to get the return value is converted into an XML document fragment (SOAP message, output message)
7. The server passes the XML fragment to the client through the network
8. The client receives an XML document fragment
9, the client parses the XML fragment, extracts the data, and transforms the data into the return value of the calling Web service


From the nature of the call above, the only requirement for a language to support Web service is:
The language must support parsing, generating, and supporting network transport of XML documents.
So Web service supports cross-platform, cross-language (essentially: XML document-based data exchange)
All operations are on the server side, client side just send, parse, encapsulate, transform data
---------------------------------------------------------------------------------------
In short, the WSDL describes a Web service that contains the following 3 aspects:
* * What: The Web service contains what action
* * How: what should the Web service call
* * WHERE: The service address of the Web service (call address)


/******************************************************************************
As long as the WSDL document for the Web service is obtained, the program can then invoke the Web service
******************************************************************************/


In CXF development, special handling is required when encountering types that cannot be handled automatically by the system:
The idea is to provide a converter, which is responsible for converting the CXF type into a fixed type of CXF.
1. Use @xmljavatypeadapter to modify types that cannot be processed
When using the annotation, the value values specify a converter class
The converter is the conversion between the completion types
2, to implement their own converters, the implementation of the converter needs to develop a cxf can be done by the type


Three technical foundations of WebService:
1. WSDL
Web Service Interface:
1. Types (Standard Schema)
2, 2N of message
3, Porttype:n a operation
WEB Service implementations:
1, the binding element: N More detailed operation
2. Service: Specify the address of the Web services
2. SOAP
Root element: Envolope:
Header element +body element
By default, header elements are not mandatory, and information such as user names, passwords, and so on can be placed in the header.
If used correctly, the BODY element content should conform to the format required by the WSDL, and if the error is called, the BODY element content is the fault child element.


A problem that Web service urgently needs to solve? How to collect money? --How to control permissions
Solution Ideas:
The server requires the client to send the input message inside must carry the user name, password information, if not or incorrect, then access denied
(If the framework is not CXF, the SOAP message generation, parsing and so on are controlled by the program, whether the addition of user name, password, or extract user name, password and other information can be done by the program code)
If you use the CXF framework, the generation and parsing of SOAP messages is done through the CXF framework.


In order for the program ape to be able to access and modify the XML (SOAP message) generated by the CXF framework, CXF provides interceptors.


Add interceptors to the server:
1. Get the Publish method return value for server endpoint
2. Call the method's Getininterceptors (), Getoutinterceptors () method to get the in and out interceptor list, then add the Interceptor



Client Add Interceptor:
1. Call Clientproxy's Getclient (HW) method, which takes the proxy of the remote Web service as the parameter and returns the value as the client object
2. Call the Client object's Client.getininterceptors (). Add (E), Client.getoutinterceptors (). Add (E) method adds an interceptor


Helloworldws factory = new Helloworldws ();
HelloWorld HW = Factory.gethelloworldwsport ();//The Proxy for the remote Web service is returned here
Client client = clientproxy.getclient (HW);
Client.getininterceptors (). Add (e);
Client.getoutinterceptors (). Add (e);


Custom interceptors:
Need to implement interceptor interface, the actual general direct inheritance Abstractphaseinterceptor abstract class


Public Class Authinterceptor extends abstractphaseinterceptor<soapmessage>{


Public Authinterceptor () {
Displays the parameter constructor that invokes the parent class, and once the parent class constructor is displayed, the program will not implicitly invoke the parent class without a parameter constructor
Super (Phase.pre_invoke); The interceptor will intercept the SOAP message before "call"
}


The Handlemessage method needs to be implemented, and its formal parameter is the intercepted SOAP message msg
Once the program gets the SOAP message, the rest of it can parse the SOAP message or modify the SOAP message
@Override
public void Handlemessage (SoapMessage msg) throws fault{
SYSTEM.OUT.PRINTLN (msg);
}
}


CXF and Spring Integration mode one (exposing the local Web service interface)
On the basis of traditional Java EE, add a layer of Web service layer.
The Java EE application now exposes the Web service, allowing any platform, any language to invoke the Java EE


Add a Web service step based on a traditional SSH project (note to have STRUTS2 release all Web service requests):
1. Copy cxf jar package, Spring jar Package
2. Configuring the CXF Core controller in the Web. xml file


<!--The following configuration indicates that all requests from/webservice/* are given to Cxfservlet processing
<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>/webservice/*</url-pattern>
</servlet-mapping>
3. In spring configuration file, import CXF to provide schema + XML configuration file


Xmlns:jaxws= "Http://cxf.apache.org/jaxws"


Xsi:schemalocation= "... Http://cxf.apache.org/jaxws
Http://cxf.apache.org/schemas/jaxws.xsd "


There are two classes of class loading paths for Ps:web applications:
1. Web-inf/classes Catalogue
2. Web-inf/lib Catalogue


<import resource= "Classpath:meta-inf/cxf/cxf.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-servlet.xml"/>
<import resource= "Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>


4. Use the Jaxws:endpoint element in the spring configuration file to expose the Web service

<!--implementor Specifies the WebService service provider, which supports two forms:
A, the class name of the directly given service provider is the interface implementation class
B, set to a bean in the container (dependency injection), to add a # number before the Bean's ID
-


A:
<bean id= "userservice" class= "org. UserService "></bean>
<jaxws:endpoint
Implementor= "Org.xxx.Class"
address= "/webservice" >
</jaxws:endpoint>


B:
<bean id= "userservice" class= "org. UserService "></bean>
<bean id= "Helloworldws" class= "Org.java.HelloWorldWs"
p:us-ref= "UserService"/>


</bean>
<jaxws:endpoint
Implementor= "#HelloWorldWs"
address= "/webservice" >
</jaxws:endpoint>


5. Add server-side interceptors
Add Jaxws:ininterceptors interceptors inside the Jaxws:endpoint


<jaxws:endpoint id= "HelloWorld"
Implementor= "#HelloWorldWs"
address= "/webservice" >


<jaxws:inInterceptors>
<bean class= "Someinterceptors"/>
<ref bean= "Anotherinterceptors"/>
</jaxws:inInterceptors>


</jaxws:endpoint>


CXF and Spring Integration Mode II (call Remote Web service)
To directly invoke the Remote Web service proxy object's method
1. The interface to the remote Web service since our action
2. Copy cxf jar package, Spring jar package, struts jar package, and integration package
3. In spring configuration file, import CXF to provide schema + XML configuration file
4. Use the jaxws:client element in the spring configuration file to configure the Remote Web service proxy
5. Adding client Interceptors
Add Jaxws:ininterceptors interceptors inside the jaxws:client


<jaxws:client id= "xxx" serviceclass= "xxx" address= "xxx" >


</jaxws-client>

WebService with Notes

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.