Web Service Notes-fkjava, service-fkjava
===================================== First lecture ====================== ========= WebService overview ==================================== ===================================== Web Service is not a framework, it is not even a technology, but a cross-platform and cross-language standard. Actual use of Web Service: 1. Integration of new and old systems of the same company. 2. Business integration of different companies. 3. Content Aggregation. CXFAxis (Apache) --> Axis2 (Apache) XFire --> CXF (XFire + Celtrix) XFire (Web Service framework) Celtrix (ESB framework) SOA (Service-Oriented Architecture) -- all components are plug-and-play. 1. various components that provide services. (Web Service) 2. enterprise Service Bus (ESB) CXF claims that the SOA framework CXF has a built-in Jetty Web Server ============================ Lecture 2/Lecture 3 ==== ============================== use CXF to develop the WebService server and client ================ ========================================================== ===== using CXF to develop a Web Service Server: /**************** each WebService component requires two components: interface and implementation class */1) Develop a Web Service business interface. The interface is modified with @ WebService. 2) develop a Web Service implementation class. Use @ WebService to modify @ WebService (endpointInt Erface = "com. lr. helloWorld ", ServiceName =" MyServiceName ") 3) Publish WebService -------------------------------------------- develop Web Service client with CXF: 1) Use wsdl2java tool provided by CXF, generate the corresponding java code WSDL-Web Service Definition Language according to the WSDL document. any Language that implements Web Service must provide and expose the WSDL document wsdl2java http: // 192.168.10.254/fkjava? Wsdl2) Find the class generated by wsdl2java, a class that inherits the Service. Instances of this type can be used as factory instances. 3) Call the getXXXPort method of the instance of the Service subclass and return the remote Web Service proxy (). ====================================== Fourth lecture ====================== ================== use CXF to process the form parameters and return values of the JavaBean compound type and List set type ============ ========================================================== ======= parameter/return value 1) it can be easily processed when the type of the form parameter/return value is String and basic data type; 2) when the form parameter/return value type is a javabean compound class, List set, array, and so on; in actual projects, the Web Service component does not implement its own business functions. It only calls the method of the business logic component to expose the javabean generated by the Web Service wsdl2java without a parameter constructor, javabean should implement the equals method to search for ==================================== lecture 5/Lecture 6 = ==================== ========= WebService core ==================================== ================================= /********* * *********************** two attributes in XML: targetNamespace-equivalent to package in java different namespaces in different files xmlns-equivalent to importxmlns = "targetNamespace" xmlns: p = "targetNamespace" p in java can be written as code, in order to use ********************************/three foundations of Web Service 1. WSDL: Web Service Definition Language (Web Service Definition Language). The WSDL document describes the following three aspects of WebService:-W HAT: What operations does the WebService contain-HOW: HOW to call the operations of the WebService-WHERE: the service address of the WebService/******************************* as long as the Web service WSDL document, next, the program can call the Web Service *******************************/------------------------------------------- web Service Interface-types (Standard Schema) -message (2N)-portType (N operation) Web Service implementation-binding (N more detailed operation)-service (specify the server name/address of the Web Service )------------------------------------------ -One WebService call-in fact, it is not a method call, but a SOAP message (XML fragment); the essence of one Web Service call: 1. the client converts the call method parameter to an XML file segment (SOAP message). This file segment must conform to the format defined in the WSDL. 2. the XML file segment is transmitted to the server over the network; 3. the server receives XML file fragments. 4. the server parses XML file fragments, extracts the data, and converts the data to the parameter values required to call the Web Service. 5. server execution method; 6. convert the return value obtained by the execution method to generate an XML document segment (SOAP message). The segment must conform to the format defined by the WSDL. 7. the XML file segment is transmitted to the client over the network; 8. the client receives XML file fragments. 9. the client parses XML file fragments, extracts the data, and converts the data to the return value of the Web Service. From the nature of the above calls, the only requirement for a language to Support Web Services is: this language supports XML File Parsing and generation, supporting network transmission. 2. SAOP: Simple Object Access Protocol (Simple Object Access Protocol) ======================================= Lecture 7 ==================== ====================== processing parameters and return values of composite data types such as Map and CXF that cannot be automatically converted ========== ========================================================== ========== in CXF development, if a type cannot be automatically processed by the system, you need to provide a converter (inheriting the XmlAdapter) to convert the type that CXF cannot automatically process to the type that can be processed. 1. use the @ XmlJavaTypeAdapter Annotation to modify the types that CXF cannot automatically process. When using this Annotation for processing, you need to specify a converter 2. to implement your own converter, you need to develop a type that CXF can process; ========================================= lecture 8/Lecture 10 ============== =================== CXF interceptor ================================ ============================================================== urgently needed solutions for Web Services problems: how to control permissions? Idea: the server requires the input message to carry the username and password information; otherwise, the call is rejected. Without a framework such as CXF, the programmer is responsible for generating and parsing SOAP messages. Therefore, whether it is adding username and password information or extracting username and password information, it can be completed by the programmer's code. If CXF and other frameworks are used, the generation and resolution of SOAP messages are completed by CXF and other frameworks. To allow programmers to access and modify the XML (SOAP message) generated by the CXF framework, CXF provides an interceptor. ** Add an interceptor to the server. 1. Obtain the returned value of the publish method of the Endpoint. 2. Call the getInInterceptors () and getOutInterceptors () Methods returned by the method to obtain the In and Out interceptor lists and add interceptors;
// Call the publish method of Endpoint to publish the Web Service
EndpointImpl ep =EndPoint.publish("http://10.1.14.6");
// Add an In interceptor
ep.getInInterceptors().add(e);
// Add an Out interceptor
ep.getOutInterceptors().add(e);
** Add an interceptor to the client. 1. Call the ClientProxy's getClient method. Call this method with the remote WebService proxy as the parameter;
2. Call the getInInterceptors () and getOutInterceptors () Methods of the Client object to obtain the In and Out interceptor lists and add interceptors;
HelloWorldWs factory =newHelloWorldWs();
// The remote WebService proxy is returned.
HelloWorld hw = factory.getHelloWorldPort();
Client client =ClientProxy.getClient(hw);
client.getInInterceptors().add(e);
client.getOutInterceptors().add(e);
** Custom Interceptor: You need to implement the Interceptor interface. In fact, it generally inherits AbstractPhaseInterceptor CXF, which has implemented some interceptors but is not mentioned in the API documentation;
// Add the verification Header Interceptor to the server
// With PhaseInterceport, you can specify the stage at which the interceptor works.
publicclassAuthInterceptorextendsAbstractPhaseInterceptor<SoapMessage>{
publicAuthInterceptor(){
// This interceptor will intercept before "calling"; Phase is a stage constant
super(Phase.PRE_INVOKE);
}
// Implement the handleMessage method when implementing your own interceptor
// The form parameter in the handleMessage method is the intercepted SOAP message.
// Once the program obtains the SOAP message, it can parse or modify the SOAP message
@Override
publicvoid handleMessage(SoapMessage msg)throwFault{
// Obtain all the headers of the soap message
List<Header> headers = msg.getHeaders();
}
}
// Add the Header message Interceptor to the client
publicclassAddHeaderInterceptorextendsAbstractPhaseInterceptor<SoapMessage>{
privateString userId;
privateString userPass;
publicAddHeaderInterceptor(String userId,String userPass){
Super (Phase. PREPAR_SEND); // called when sending preparation
this.userId = userId;
this.userPass = userPass;
}
@Override
publicvoid handleMessage(SoapMessage msg)throwsFault{
List<Header> headers = msg.getHeaders();
// Wrap the ele element into a Header and add it to the Header list of the SOAP message
headers.add(newHeader(newQName("fkjava"),ele));
}
}
=============================== Lecture 9/tenth/11th ====================== ========= detailed explanation of the Web Service core SOAP protocol =========================== ====================================== add In/Out logs to the server interceptor, from log analysis; optional SOAP Header, added under the control of a programmer;
The Body element is always default. There are two possible situations in the Body element:
-When the Web Service interaction is correct, the content in the Body element is controlled by the WSDL;
-When a Web Service interaction error occurs, the Body element is Fault.
========================================== 12th lecture/13th lecture ============ ==================== CXF and Spring integration ============================= ========================================================== = You can add a Web Service layer based on traditional Java EE applications. Our Java EE application can be exposed as a Web Service, which allows programs written in any language on any platform to call this Java EE application. Step 1. Add WebService to the traditional SSH project. copy the Jar package of CXF (six at the core) 2. in the web. configure the core controller of CXF in xml: CXFServlet; 3. add the CXF Schema and XML configuration file to the Spring configuration file. 4. expose WebService by using the jaxws: endpoint element in the Spring configuration file; 5. to add an interceptor, add the inInterceptors and outInterceptors sub-elements to the jaxws: endpoint element. ** implementor specifies the service provider of the WebService. Two methods are supported:. directly specify the Class Name of the server provider (not recommended); <javws: endpoint implementor = "org. lr. cxf. ws. impl. helloWorldWs "address ="/lrservice "> </javws: endpoint> B. set as a bean in the container. <bean id = "helloWorldWs" class = "org. lr. cxf. ws. impl. helloWorldWs "p: us-ref =" userService "/> <! Before -- id, add # --> <javws: endpoint implementor = "# helloWorldWs" address = "/lrservice"> </javws: endpoint ===================================== 14th lecture ============== ============================= the second method of integration of CXF and Spring _ making JavaEE applications dependent on third-party WebService-that is, calling WebService = ========================================================== =================================== 1. copy the Jar package of CXF (six at the core) 2. in the web. configure the core controller of CXF in xml: CXFServlet; 3. add the CXF Schema and XML configuration file to the Spring configuration file. 4. use the jaxws: client element in the Spring configuration file to call WebService; 5. to add an interceptor, add the inInterceptors and outInterceptors sub-elements to the jaxws: client element. ** to ensure that the Web Service proxy object can be automatically assembled to the Action, ensure that the property value of this id corresponds to the setter method name in the Action <jaxws: client id = "hw" serviceClass = "org. lr. cxf. ws. helloWorld "address =" http: // 192.168.10.254: 9999/lrservice "> </jaxws: client>
From Weizhi note (Wiz)
Attachment List
- Wsdl .png
- Interceptor .png