"Java EE Learning Day 81st" "CXF framework" "CXF Integration Spring"

Source: Internet
Author: User
Tags wsdl java se

I. Introduction of CXF

CXF is a project under the Apache Company, Cxf=celtix+xfire, which supports soap1.1, soap1.2, and the ability to quickly and seamlessly integrate with spring.

In addition Jax-WS is a set of standards for developing webservice services released by Sun. Early standards such as JAX-RPC have rarely been used, and CXF is the WEBSERVICE,JAX-WS developed under the new standard JAX-WS built into the jdk1.6.

CXF official:http://cxf.apache.org/download.html

After the download is complete, unzip the compressed file, you can find a Samples folder, in this folder gives a lot of examples used to study the use of CXF. The ant configuration Run method is provided in CXF2.4.0, so if you want to run the sample program quickly, you need to install the ant environment, of course, the Tomcat environment, CXF environment is certainly necessary.

Ant and Tomcat are all Apache projects:

Ant:http://ant.apache.org/

Set Environment variables:

java_home   =%java_home%\bin;%cxf_home%\bin;%catalina_home%\bin;%ant_home%\binclasspath=.; %cxf_home%\lib\cxf-manifest.jar;. \build\classes

Using Ant to run the first CXF example (take 2.4.0 as an example), open the Samples/java_first_pojo folder and open the two command-line windows in that folder, entering

Ant Server

And

Ant Client

You can run the example and see the console print results for both the server and the client, but note the JDK version problem, preferably using the jdk1.6, in the jdk1.8 environment to run, jdk1.7 did not try.

II. Application of building cxf under Java se

1. Preparation: The Lib folder all the jar packages are copied to the project, and added to the Classpath,jar package there are many, including the future needs of the Springjar package.

2. After the jar package has been copied and tested, there are two ways to publish the WebService service, one is "simple service CXF" and the other is "complex Service Release"

(1) Simple Service Release

Using the Serverfactorybean class implementation, this class is the core class.

1  PackageCom.kdyzm.cxf.ws.server;2 3 ImportOrg.apache.cxf.frontend.ServerFactoryBean;4 5  Public classCxfoneserver {6      Publicstring SayHello (string hello) {7SYSTEM.OUT.PRINTLN ("The requested parameter was received:" +hello);8         returnHello;9     }Ten      Public Static voidMain (string[] args) { OneServerfactorybean bean=NewServerfactorybean (); ABean.setaddress ("Http://localhost:9090/hello"); -         //Sets the service interface, or the service class if there is no interface -Bean.setserviceclass (Cxfoneserver.class); the         //Setting the Service implementation class -Bean.setservicebean (Newcxfoneserver ()); - bean.create (); -SYSTEM.OUT.PRINTLN ("Service released successfully!") "); +     } -}

The use of this class is characterized by:

* Service class may not use @webservice annotations

* Even if the service class does not provide external services, the service can also be published successfully

It can be seen that publishing services in a CXF environment and publishing services in a JDK environment are distinct.

(2) Complex Service Release (recommended Service Release method)

The core class used by complex service publications is the Jaxwsserverfactorybean class, which is a subclass of the Serverfactorybean class, and also a feature extension class, which is recommended because the WSDSL file generated by the service published by the class is more canonical.

1  PackageCom.kdyzm.cxf.ws.server;2 3 ImportJavax.jws.WebService;4 ImportJavax.xml.ws.BindingType;5 Importjavax.xml.ws.soap.SOAPBinding;6 7 ImportOrg.apache.cxf.interceptor.LoggingInInterceptor;8 ImportOrg.apache.cxf.interceptor.LoggingOutInterceptor;9 ImportOrg.apache.cxf.jaxws.JaxWsServerFactoryBean;Ten  One /** A * The second method of publishing the service, which must be annotated with WebService, otherwise the methods in the service class cannot be exposed -  * @authorKdyzm - * It is best to use SOAP1.2 so that the SOAP1.1 client can also access the service normally the  */ - @WebService -@BindingType (value=soapbinding.soap12http_binding) -  Public classCxftwoserver { +      Publicstring SayHello (string hello) { -SYSTEM.OUT.PRINTLN ("Get request parameter:" +hello); +         returnHello; A     } at      PublicString Calculate (intinput) { -         returnInput*input+ ""; -     } -      Public Static voidMain (string[] args) { -Jaxwsserverfactorybean bean=NewJaxwsserverfactorybean (); -Bean.setaddress ("Http://localhost:9000/helloworld"); inBean.setserviceclass (Cxftwoserver.class); -Bean.setservicebean (Newcxftwoserver ()); to         //With the log option, you can clearly see the request and the corresponding code.  +Bean.getininterceptors (). Add (Newloggingininterceptor ()); -Bean.getoutinterceptors (). Add (Newloggingoutinterceptor ()); the bean.create (); *System.out.println ("The second way the service was released successfully!") "); $     }Panax Notoginseng}

The way to publish a service using the Jaxwsserverfactorybean class is characterized by:

* The service class must use @webservice annotations, although not adding the annotations will not error, but all service methods in the service class will not be exposed (the WSDL file does not find the corresponding method), so that the service class is not really useful.

* This class is the same as the Serverfactorybean class, and even if there is no external exposure service method, the service can be published successfully.

3. Description

(1) The service class can be described using the relevant annotations, and it is strongly recommended to use annotations @bindingtype (value=soapbinding.soap12http_binding) to declare the service as a service that complies with the SOAP1.2 specification.

(2) by adding two lines of code can listen to the request and response process, so that the very clear view of the request header information, request body information, response header information and response body information.

Bean.getininterceptors (). Add (new  loggingininterceptor ()); Bean.getoutinterceptors (). Add (  New Loggingoutinterceptor ());

    

(3) The service published using CXF can generate the calling code through the Wsimport command, in fact, this kind of thing does not need to repeat, after all, webservice the biggest selling point is this.

However, it is important to note that the Wsimport command only recognizes SOAP1.1, so if the service is SOAP1.2, then using the Wsimport command will not work. The solution is to use the CXF framework to provide

The Wsdl2java command, which functions like the Wsimport command, is more powerful than the Wsimport command, and it supports SOAP1.2.

How to use:

Wsdl2java              -Thed parameter, which specifies the code        -generated catalog     -p parameter, specifies the new package structure that is generated.   

Examples of how to use:

Wsdlwjava-d. -P com.kdyzm.ws.cxf.server http://localhost:9090/ws?wsdl    

Third, CXF integration spring

Now it's clear, what everything has to be integrated with sprig, hibernate can not be used, because there are many similar frameworks to use, such as the MYBATIS;STRUTS2 framework can not, but only this spring is necessary to use. This shows how important the status of spring is.

The jar package is the same as before, and all the jar packages under the/lib directory are copied to the Web-inf/lib folder. Integration steps:

First step: Configure the Web. XML configuration file

<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>/cxf/*</url-pattern></servlet-mapping>

Note The code for the black Background section, which is how the interception rule is, and the "address" attribute part of the configuration in the spring configuration file later is/cxf/, not all, which requires special attention.

Step Two: Configure the CXF configuration file

The CXF configuration file is actually the spring configuration file.

    

"Java EE Learning Day 81st" "CXF framework" "CXF Integration Spring"

Related Article

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.