CXF Integrated Spring Complete Example

Source: Internet
Author: User
Tags soap wsdl

1 CXF Overview


1.1 CXF Introduction

Apache CXF =celtix + Xfire,apache CXF, formerly known as Apache Celtixfire, has now been formally renamed Apache CXF, hereinafter referred to as CXF. CXF inherits the essence of the two open source projects of Celtix and XFire, provides full support for Jax-WS, and provides a variety of Binding, DataBinding, Transport, and various Format support, and can be adapted to the needs of the actual project. Code first, or WSDL first, makes it easy to publish and use Web Services. Apache CXF is already an official Apache top project.

Apache CXF is an open Source services framework that CXF helps you build and develop services using Frontend programming APIs like Jax-ws. These services can support multiple protocols, such as SOAP, Xml/http, RESTful HTTP, or CORBA, and can run on a variety of transport protocols, such as HTTP, JMS, or JBI,CXF, which greatly simplifies the creation of Services and Inherits from the XFire tradition, as can be seamlessly integrated with Spring naturally.

2 CXF version of HelloWorld

We use the MyEclipse integrated development environment to learn CXF.

2.1 Create a new WebProject and join the dependency package

2.2 Write a service interface

The first is the service interface:

The interface here needs to use the annotation @WebService description, the interface method if there are parameters, you need to use @webparam to decorate, and use the property name to define the name, otherwise, after publishing, the parameters will use the default Args0 ...

[Java] view Plaincopyprint?

    1. package ws;

    2. import Javax.jws.WebService;

    3. import Ws.medo.User;

    4. @WebService

    5. Public Interface HelloWorld {

    6. Public User SayHello (String name,user User);

    7. }

Package Ws;import Javax.jws.webservice;import ws.medo.User, @WebServicepublic interface HelloWorld {public User SayHello (String name,user User);}


Next, write an implementation class for the interface:

[Java] view Plaincopyprint?

  1. Package Ws.impl;

  2. import Javax.jws.WebService;

  3. import ws. HelloWorld;

  4. import Ws.medo.Cat;

  5. import Ws.medo.User;

  6. @WebService (Endpointinterface = "ws.") HelloWorld ", servicename=" Hellogt")

  7. Public class Helloworldimpl implements helloworld{

  8. @Override

  9. Public User SayHello (String name, user user) {

  10. System.out.println (User.getname ());

  11. User.getlist (). Add (new Cat ("1"));

  12. User.getlist (). Add (new Cat ("2"));

  13. return user;

  14. }

  15. }

Package Ws.impl;import Javax.jws.webservice;import ws. Helloworld;import ws.medo.cat;import Ws.medo.User; @WebService (endpointinterface = "ws. HelloWorld ", servicename=" Hellogt ") public class Helloworldimpl implements helloworld{@Overridepublic User SayHello ( String name, user user) {System.out.println (User.getname ()); User.getlist (). Add (New Cat ("1")); User.getlist (). Add (New Cat ("2")); return user;}}


2.3 Declaration Service

Create a new Beans.xml under the Web-inf folder.

This is a spring configuration file that first imports several XML files used by CXF and then defines the service we just wrote.

[HTML] view Plaincopyprint?

  1. <? XML version="1.0" encoding="UTF-8"?>

  2. < Beans xmlns="Http://www.springframework.org/schema/beans"

  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http:/ /cxf.apache.org/jaxws "

  4. xsi:schemalocation= "

  5. Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

  6. Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">

  7. < Import Resource="Classpath:meta-inf/cxf/cxf.xml" />

  8. < Import Resource="Classpath:meta-inf/cxf/cxf-extension-soap.xml" />

  9. < Import Resource="Classpath:meta-inf/cxf/cxf-servlet.xml" />

  10. < Bean id="Service1" class="Ws.impl.HelloWorldImpl"> </Bean>

  11. < Jaxws:endpoint id="Hello" implementor= "#service1" address="/helloworldservice" />

  12. </ Beans >

<?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" xsi:schemalocation= " Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp:// Cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "><import resource=" classpath:meta-inf/cxf/ Cxf.xml "/><import resource=" Classpath:meta-inf/cxf/cxf-extension-soap.xml "/><import resource=" Classpath:meta-inf/cxf/cxf-servlet.xml "/><bean id=" Service1 "class=" Ws.impl.HelloWorldImpl "></bean ><jaxws:endpoint id= "Hello" implementor= "#service1" address= "/helloworldservice"/></beans>


2.4 Configuring the Servlet

Let's take a look at the configuration of Web.

[HTML] view Plaincopyprint?

  1. <? XML version="1.0" encoding="UTF-8"?>

  2. < Web-app version="2.5" xmlns="Http://java.sun.com/xml/ns/javaee"

  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"

  4. xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee

  5. Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">

  6. < Listener >

  7. < Listener-class >org.springframework.web.context.ContextLoaderListener</listener-class >

  8. </ Listener >

  9. < Context-param >

  10. < Param-name >contextconfiglocation</param-name>

  11. < Param-value >/web-inf/beans.xml</param-value>

  12. </ Context-param >

  13. < servlet >

  14. < Servlet-name > HelloWorldService</servlet-name>

  15. < Servlet-class >org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

  16. < Load-on-startup >1</load-on-startup>

  17. </ servlet >

  18. < servlet-mapping >

  19. < Servlet-name > HelloWorldService</servlet-name>

  20. < Url-pattern >/services/*</url-pattern>

  21. </ servlet-mapping >

  22. < welcome-file-list >

  23. < Welcome-file >index.jsp</welcome-file>

  24. </ welcome-file-list >

  25. </ Web-app >

<?xml version= "1.0"  encoding= "UTF-8"? ><web-app version= "2.5"  xmlns= "/http Java.sun.com/xml/ns/javaee "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation="/HTTP/ Java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><context-param><param-name>contextconfiglocation</param-name><param-value>/web-inf /beans.xml</param-value></context-param><servlet><servlet-name>helloworldservice</ servlet-name><servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class>< Load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name> Helloworldservice</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping ><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app> 


2.5 Deploying a project to Tomcat

After the project is successfully deployed to Tomcat, launch Tomcat to access

Http://localhost:8080/Cxf_webService/services, you will see the page shown in 3

OK server side on the job Gaocheng ..... ..... ....... ...................

2.7 Client

1, first create the client project, and then import the jar package as above, without the spring

2, gets the service-service interface class (similar to the. h header file in C + +)

1)install CXF, set environment variables, such as: d:/apache/apache-cxf-2.2.4; At the same time, add ";%cxf_home%/bin" (optional) after path. The usage of Wsdl2java is as follows:
wsdl2java–pPackage name –d Directory name WSDL path
such as:wsdl2java–p demo.service.client–d e:/src htt://localhost:8080/helloworld?wsdl
-p Specifies the namespace of its WSDL, which is the package name to generate code for
- DSpecifies the directory where the code is to be generated
-clientgenerate the code for the Client test Web service
-serverbuild the code for the server to start the Web service
-implgenerate the implementation code for the Web service
-antGenerate Build.xml file
-compilecompile after generating code
-quientsilent mode, does not output warnings and error messages
-allgenerate all Start endpoint codes: Types,service Proxy,service interface, server mainline, client mainline, implementation object, and an Ant Build.xml file.

2) execute the wsdl2java batch process, such as:
Wsdl2java-p cxf.test-d D:/src-server http://localhost:8080/CXFTomcat/services/HelloWorld?wsdl

3) Import the Java interface class into the project.
The Java class files generated in the previous step are many, as long as the generic application will import the class file that describes the interface to the project, such as the Helloworld.java file generated in the previous example.

Note: After some versions are copied, super () in the class will make an error, plus-frontend JAXWS21

This example is//wsdl2java-frontend jaxws21–p gt.client–d e:\myeclipse\Cxf_Client\src http://localhost:8080/Cxf_ webservice/services/helloworldservice?wsdl

After the copy is complete, such as:

3, calling the class

[Java] view Plaincopyprint?

  1. import Gt.client.HelloGT;

  2. import Gt.client.HelloWorld;

  3. import Gt.client.User;

  4. Public class Clientstartmain {

  5. //wsdl2java-frontend jaxws21–p gt.client–d e:\myeclipse\Cxf_Client\src http://localhost:8080/Cxf_webService/ SERVICES/HELLOWORLDSERVICE?WSDL

  6. Public Static void Main (string[] args)

  7. {

  8. Hellogt gt=New hellogt ();

  9. HelloWorld Hello=gt.gethelloworldimplport ();

  10. User user=new user ();

  11. User.setname ("123");

  12. //system.out.println (Hello.sayhello ("1", user));

  13. System.out.println (Hello.sayhello ("1", user). GetList (). Size () +":"+hello.sayhello ("1" , user). GetList (). Get (0). GetName ());

  14. }

  15. }

Import Gt.client.hellogt;import Gt.client.helloworld;import Gt.client.user;public class Clientstartmain {//wsdl2java -frontend jaxws21–p gt.client–d e:\myeclipse\Cxf_Client\src http://localhost:8080/Cxf_webService/services/ Helloworldservice?wsdlpublic static void Main (string[] args) {Hellogt gt=new hellogt (); HelloWorld Hello=gt.gethelloworldimplport (); User User=new User (), User.setname ("123"),//system.out.println (Hello.sayhello ("1", user)); System.out.println (Hello.sayhello ("1", user). GetList (). Size () + ":" +hello.sayhello ("1", user). GetList (). Get (0). GetName ());}}


Print the contents as follows:

2013-2-23 13:03:35 Org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildservicefromwsdl
Info: Creating Service {Http://impl.ws/}hellogt from wsdl:http://localhost:8080/cxf_webservice/services/ helloworldservice?wsdl
2:1

OK, the client is OK.

CXF Integrated Spring Complete Example

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.