Develop WebService with CXF

Source: Internet
Author: User
Tags webservice annotation wsdl

Develop WebService1 with CXF. What is CXF

Apache CXF =celtix + Xfire

Supports multiple protocols:

? soap1.1,1,2

? Xml/http

? CORBA (Common objectrequest Broker Architecture Common Object Request Broker architecture, WS used in earlier languages. c,c++,c#)

? And can be quickly and seamlessly integrated with spring

? Flexible deployment: Can be run on Tomcat,jboss,jetty (built-in), Ibmws,beawl above.

2. Install additional support items for CXF

Ant

Tomcat

and set the following environment variables:

? Java_home

? Cxf_home

? Ant_home

? Catalina_home

? Path =%java_home%\bin;%cxf_home%\bin;%catalina_home%\bin;%ant_home%\bin

? Classpath=.; %cxf_home%\lib\cxf-manifest.jar;. \build\classes

Attention:

1. Why use ant as a tool? Ant has been widely used as a tool and has a long history. With Ant's built-in commands, you can compile Java source files (javac), run Java files (Java), Package class files (jar, war, Ear), create (mkdir), delete (Del), copy (copy), You can even use ant to execute SQL files. Because Ant is a file written in an XML language, it is named Build.xml file by default. So, in the future, you should know that this is an ant file when you see the file named Build.xml.

2. The default is built-in support for Ant in Eclipse and MyEclipse. You can create a file called Build.xml file in any project in MyEclipse, and you'll find it has a different icon (with a small ant on it).

3. Using ant to construct the first CXF example

Start the server in the Cxf_home\samples\java_first_pojo directory:

? Enter the above directory in command-line mode:

? Execute the following command ant server

Open a new command-line window under the same directory to run:

? Ant Client

Attention:

1, Java_frist_pojo is located in the samples under the simplest of a project.

2, Ant Server, is to execute a piece of code in the Build.xml configuration file through ant, the job is to compile the Java class and start the service.

3. Ant client is the Access service.

4, let us take a look at its source code. Composed of three classes: interface HelloWorld, implementing Class Helloworldimpl and service publishing Class Server

At the other end there is only one Client.java file.

Execute ant is recompiled.

Ant Server is the startup service.

The ANT client is running the client.

Ant Deploy–dtomcat=true

4. Use ant to publish it to the Tomcat server

1. Execute ant clean to clear the previously generated code

2. Execute the Ant War package for this app.

3. Execute ant deploy–dtomcat=true to publish the project to Tomcat's server.

4. Start the Tomcat server

5. Open the address bar of IE to enter:

http://localhost:9999/helloworld/services/hello_world?wsdl

Or: Http://localhost:9999/helloworld/services

6. Execute at the command line:

? Ant client-servlet–dbase.url=http://localhost:9999

? Or visit this webservice in the webserviceexplorer of MyEclipse

7. Stop the Tomcat server.

8. Uninstall application: Ant Undeploy–dtomcat=true

9. Clear the project: Ant clean

5. Developing CXF's javase applications in eclipse

Import all the jar files in the CXF. You can use Wach_jars to observe the purpose of each jar file.

Attention:

1. Remember to adjust your JDK version to jdk1.6.0_24.

2. Copy all CXF jar packages to the project. Added to the classpth.

3, before the development can read the Cxf_home\lib\watch_jars file, analysis of the dependencies between jar files

6.CXF Publishing a service class

Publish your app in two different classes:

Serverfactorybean--Facotrybean

Jaxwsserverfactorybean (recommended for use with this class)

7. Service and client class invocation principle diagram

8. Publishing services using Serverfactorybean

Use Serverfactorybean to publish the service.

Use CXF to publish a service that is completely different from JDK6 publishing a service

? * Can be published successfully even without the use of @webservice annotations

? * Even if this class is not published as a method to publish success

9. Using the Clientproxyfactorybean client call

After using Serverfactorybean to publish a service, the client code generated using Wsimport can be successfully invoked without an interface.

However, if you want to use the Clientproxyfactorybean client to invoke the server, you must first create an interface on the server side. (Spring has always required interface-oriented programming, and CXF and spring are well integrated, and that's where it's going.) ), so you have to rewrite the server-side code. This causes the calling code that you just generated with wsimport to fail.

At the same time, Clientproxyfactorybean is importing all of the CXF's packages due to the use of the CXF environment.

At the same time, if called in this project, you can use the interface of this project directly.

If called in other projects, the generated class still needs to be wsimport, but only one interface is required.

10. Use Jaxwsserverfactorybean to publish the service: (This class is recommended)

Jaxwsserverfactorybean is a subclass of Serverfactorybean and is also a feature extension class.

However, this API is not available in the API documentation for CXF, please obtain help for this class by looking at the source code.

In this class, you must add the @webservice annotation on the class that is being published as a service, and if it is not annotated, it will not expose any methods, although it is not an error.

wsdl files generated using this class are more canonical.

11. Publish a service using the CXF class

after a successful creation, use Wsimport The generated client code can also be called successfully

when using CXF when publishing a service, you first need to write an interface to declare the service type. This is somewhat similar to the implementation of the service layer in Spring

12. Using the Jaxwsproxyfactorybean client call

This call process, like the requirements of Clientproxyfactorybean, must also have an interface.

At this point, you can still use the Wsimport to build the interface in other project calls.

Called in another project:

13.wsdl2java Generating Customer Code

In CXF, a tool is also provided to generate the client calling code. It functions like a wsimport.

This tool is located in the Cxf_home/bin directory. Parameters differ from Wsimport.

It contains the following parameters:

? -d parameter, which specifies the directory that the code generates.

? -P parameter, which specifies the new package structure that is generated.

It is necessary to note that since Wsdl2java is a native code generated from jdk1.7 , the generated code needs to be modified a little bit.

Execute at the command line:

Wsdl2java–d . http://127.0.0.1:6666/helloworld?wsdl

Call-side code:

14. Add a message blocker to the service

loggingininterceptor– information input Blocker – request

loggingoutinterceptor–-response For information output

Add interceptors to the server, similar to the process of capturing SOAP messages using TCP/IP monitor.

15. Webservie can also be accessed using JavaScript

jquery accesses WebService.

? 1. Prepare jquery.js in this domain (jquery is limited to cross-domain requests because jquery is restricted to use in this domain.) But in order to parse XML files quickly, jquery can still be used as a tool to parse XML files. )。

? 2. Initiate AJAX requests via JS.

Develop WebService with CXF

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.