JAVAEE--BOS logistics project 07: WebService entry, apache CXF entry, CXF-based CRM service, javaeecxf
1Learning plan
1. Getting started With WebService
N what is WebService
N call the WebService on the network
N concepts of SOAP and WSDL
N release a simple WebService Service Based on JDK1.7
N use the JDK wsimport command to generate local code to call the WebService Service
2. apache CXF entry
N CXF Overview
N CXF entry case (server development)
N CXF entry case (Client development)
1) Use the WSDL2Java command to generate a local code call
2) register a proxy object call in the configuration file
3. Publish CRM services based on CXF
N create a CRM database and a CRM Customer table
N release service
2
Getting started With WebService2.1
What is WebService?
Web service is a platform-independent, low-coupling, self-contained, programmable web-based application that can use open XML (a subset of standard General Markup Language) standards are used to describe, publish, discover, coordinate, and configure these applications for Distributed interoperability applications. [1]
Web Service technology enables different applications running on different machines to exchange data or integrate with each other without additional, dedicated third-party software or hardware. Applications implemented according to Web Service specifications can exchange data with each other regardless of the language, platform, or internal protocol they use. A Web Service is a self-describing and self-contained available network module that can perform specific business functions. Web services are also easy to deploy, because they are based on some common industrial standards and some existing technologies, such as the subset XML and HTTP in the standard General Markup Language. Web Service reduces the cost of application interfaces. Web Service provides a general mechanism for the integration of business processes across an enterprise or even multiple organizations.
LWebServiceFeatures
- WebService accepts client requests through HTTP POST
- XML data is transmitted between the WebService and the client using the SOAP protocol.
- It is designed for cross-platform or cross-language use.
2.2
Call WebService on the network
Http://webxml.com.cn/
2.3
Concepts of SOAP and WSDL2.3.1
SOAP (Simple Object Access
Protocol
): Simple Object Access Protocol
N as an XML-based protocol, SOAP is used to transmit data online.
N SOAP = + XML data based on HTTP.
N SOAP is based on HTTP.
The composition of u SOAP is as follows:
L Envelope-required part. It appears as the root element of XML.
L Headers-optional.
L Body-required. The body section contains the method of the server to be executed. And the data sent to the server.
Example:
POST /WebServices/IpAddressSearchWebService.asmx HTTP/1.1Host: ws.webxml.com.cnContent-Type: text/xml; charset=utf-8Content-Length: lengthSOAPAction: "http://WebXml.com.cn/getCountryCityByIp"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getCountryCityByIp xmlns="http://WebXml.com.cn/"> <theIpAddress>string</theIpAddress> </getCountryCityByIp> </soap:Body></soap:Envelope>
2.3.2
WSDL Web Service Description Language
WSDL (WebService Description Language): web Service Description Language
It is an xml document used to describe some information about the current service (Service name, service publishing address, method provided by the Service, parameter type of the method, type of Return Value of the method, etc)
3
Publish a WebService Based on jdk1.73.1
Server Release
Step 1: Create a Java Project
Step 2: Create a class and add the Webservice Annotation
Step 3: provide a method for sayHello
Step 4: Call the release service method provided by jdk in the main method
Step 5: access the service's wsdl document (Service release address +? Wsdl) http: // 192.168.115.87: 8080/hello? Wsdl
@ WebServicepublic class HelloService {public String sayHello (String name, int I) {System. out. println ("the server's sayHello method is called .... "); Return" helle "+ name;} public static void main (String [] args) {String address =" http: // 192.168.115.87: 8080/hello "; object implementor = new HelloService (); Endpoint. publish (address, implementor );}}
3.2
Client call3.2.1
Use the wsimport command in jdk
Purpose: parse the wsdl file and generate local client code.
3.2.2
Client call
1. Use the wsimport command to parse the wsdl file and generate local code
2. Create a proxy object using local code
3. Remote Call through proxy objects
/*** 1. Use the wsimport command to parse the wsdl file to generate the local code * 2. Create a proxy object through the Local Code * 3. Use the proxy object to remotely call * @ author zhaoqx ** /public class App {public static void main (String [] args) {HelloServiceService ss = new HelloServiceService (); // create a client proxy object for remotely calling HelloService proxy = ss. getHelloServicePort (); String ret = proxy. sayHello ("James", 10); System. out. println (ret );}}
4
Apache CXF entry4.1
Download
Official Website: cxf.apache.org
Download the CXF development kit:
Decompress the zip file above:
L Apache CXF = Celtix + Xfire
L supports multiple protocols:
- SOAP1.1 and 1.2
- XML/HTTP
- Common Object Request Broker Architecture: the Common Object Request proxy Architecture. WS is used in earlier languages. C, c ++, C #)
- And can be quickly and seamlessly integrated with Spring
- Flexible Deployment: it can run on Tomcat, Jboss, Jetty (built-in), IBMWS, and BeaWL.
4.2
Entry case (server development)
Step 1: create a dynamic web project
Step 2: Import jar packages related to CXF
Step 3: configure a Servlet provided by the CXF framework in web. xml
<! -- Configure the Servlet provided by the CXF framework --> <servlet-name> cxf </servlet-name> <servlet-class> org. apache. cxf. transport. servlet. CXFServlet </servlet-class> <! -- Specify the location of the configuration file of the CXF framework through initialization parameters --> <init-param> <param-name> config-location </param-name> <param-value> classpath: cxf. xml </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> cxf </servlet-name> <url-pattern>/ service/* </url-pattern> </servlet-mapping>
Step 4: Provide cxf. xml in the class path
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: jaxws = "http://cxf.apache.org/jaxws" xmlns: soap = "http://cxf.apache.org/bindings/soap" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configura Tion/soap. xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <! -- The introduction of CXF Bean definition is as follows, used in earlier versions --> <import resource = "classpath: META-INF/cxf. xml "/> <import resource =" classpath: META-INF/cxf/cxf-extension-soap.xml "/> <import resource =" classpath: META-INF/cxf/cxf-servlet.xml "/> </beans>
Step 5: Develop an interface and implementation class
Step 6: register the service in cxf. xml
4.3
Entry case (Client development)
L Method 1: Use the wsimport Command provided by jdk to generate local code to complete the call
L Method 2: Use the method provided by CXF (important)
Step 1: Create a Java project and import jar packages related to CXF
Step 2: Use wsimport or CXF to provide wsdl2java to generate local code. You only need to generate an interface file.
Step 3: copy the interface file to the Project
Step 4: Provide the spring configuration file to register the client proxy object
Step 5: Read the spring configuration file, create a spring factory, and obtain proxy objects from the factory for remote calls.
5
Develop crm services based on CXF5.1
Database environment construction
Run the SQL script:
5.2
Web Project Environment Construction
Step 1: create a dynamic web project
Step 2: Import jar packages related to CXF
Step 3: Configure web. xml
<Context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: cxf. xml </param-value> </context-param> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <! -- Configure the Servlet provided by the CXF framework --> <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>/service/* </url -pattern> </servlet-mapping>
Step 4: Provide cxf. xml in the class path
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: jaxws = "http://cxf.apache.org/jaxws" xmlns: soap = "http://cxf.apache.org/bindings/soap" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configura Tion/soap. xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> <! -- The introduction of CXF Bean definition is as follows, used in earlier versions --> <import resource = "classpath: META-INF/cxf. xml "/> <import resource =" classpath: META-INF/cxf/cxf-extension-soap.xml "/> <import resource =" classpath: META-INF/cxf/cxf-servlet.xml "/> </beans>
Step 5: Create a Customer entity class for the t_customer table
The empty Parameter Method of Customer needs to be retained. The corresponding java
Step 6: Develop an interface and implementation class
Step 7: Configure cxf. xml
<! -- Configure the data source --> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: /// crm_heima32 "/> <property name =" username "value =" root "/> <property name =" password "value =" root "/> </bean> <! -- Transaction Manager --> <bean id = "txManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <property name =" dataSource "ref =" dataSource "/> </bean> <! -- Transaction annotation is supported --> <tx: annotation-driven transaction-manager = "txManager"/> <bean id = "jdbcTemplate" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" dataSource "/> </bean> <bean id =" customerService "class =" com. itheima. crm. service. customerServiceImpl "> <property name =" jdbcTemplate "ref =" jdbcTemplate "/> </bean> <! -- Register a service --> <jaxws: server id = "myService" address = "/customer"> <jaxws: serviceBean> <ref bean = "customerService"/> </jaxws: serviceBean> </jaxws: server>