Complete example of cxf integration with spring

Source: Internet
Author: User
1 cxf Overview

1.1 cxf Introduction

Apache cxf = celtix + xfire, formerly known as Apache celtixfire, is now officially renamed Apache cxf. Cxf inherits the essence of the two open-source projects celtix and xfire, provides comprehensive support for the JAX-WS, and provides a variety of binding, databinding, transport and a variety of formats support, in addition, you can use code first or WSDL
First) to easily publish and use web services. Apache cxf is a formal Apache top-level project.

Apache cxf is an open-source services framework that helps you build and develop services like JAX-WS using frontend programming APIs. These services can support multiple protocols, such as soap, XML/HTTP, restful HTTP, or CORBA, and can run on multiple transmission protocols, such as HTTP, JMS, or jbi, cxf greatly simplifies the creation of services and inherits the xfire tradition. It can also be seamlessly integrated with spring.

 

2 cxf helloworld

We use the myeclipse integrated development environment to learn cxf.

 

2.1 create a webproject and add the dependency package

 

2.2 write a Service Interface

The first is the service interface:

The interface here needs to be described using annotation @ WebService. If there is a parameter in the method in the interface, you need to use @ webparam to modify it and use the attribute name to define the name. Otherwise, after the release, the default args0...

 

package ws;import javax.jws.WebService;import ws.medo.User;@WebServicepublic interface HelloWorld {public User sayHello(String name,User user);}

 

Next, write an interface implementation class:

 

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 declare a service

Create a new beans. xml in the WEB-INF folder.

This is a spring configuration file. First, Import several XML files used by cxf, and then define the service we just wrote.

<?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 configure Servlet

Let's take a look at the web. xml configuration.

 

<?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 deploy the project to Tomcat

After successfully deploying the project to Tomcat, start Tomcat and access

Http: // localhost: 8080/cxf_webservice/services. The page shown in 3 is displayed.

 

 

 

OK, the server is working now ................................. .........

 

2.7 Client

1. First create a client project, and then import the jar package as above, without the need for spring

2. Obtain the Service Interface Class (similar to the. h header file in C/C ++)

1) install cxf and set environment variables, such as: D:/Apache/apache-cxf-2.2.4; and add '; % cxf_home %/bin' After path (optional ). The usage of wsdl2java is as follows:
Wsdl2java-P package name-d directory name WSDL path
For example, wsdl2java-P demo. Service. Client-d e:/src htt: // localhost: 8080/helloworld? WSDL
-P specifies the namespace of the WSDL, that is, the package name of the Code to be generated.
-D specifies the directory where the code to be generated is located
-The client generates the code for the client to test the web service.
-The server generates the code for the server to start the web service.
-Impl: generate the Web service implementation code
-Ant generate the build. xml file
-Compile the code generated by compile
-Quient silent mode. No warning or error messages are output.
-All: generate all the starting endpoint codes: types, Service proxy, service interface, server mainline, client mainline, implementation object, and an ant build. xml file.

2) run the wsdl2java batch processing program, for example:
Wsdl2java-P cxf. Test-d:/src-server http: // localhost: 8080/cxftomcat/services/helloworld? WSDL

3) import the Java interface class to the project.
There are many Java class files generated in the previous step. Generally, you only need to import the class file of the interface to the project. The helloworld. Java file generated in the above example.

 

Note: after some versions are copied, super () in the class will encounter an error. Add-frontend jaxws21

// Wsdl2java-frontend jaxws21-p gt. Client-d e: \ myeclipse \ cxf_client \ SRC
Http: // localhost: 8080/cxf_webservice/services/helloworldservice? WSDL

 

After the copy is completed, see:

 

3. Call class

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());}}

 

 

The printed content is as follows:

13:03:35 org. Apache. cxf. Service. Factory. reflectionservicefactorybean buildservicefromwsdl
Information: creating service {http://impl.ws/?hellogt from WSDL:

Http: // localhost: 8080/cxf_webservice/services/helloworldservice? WSDL

2:1

 

 

OK, the client is OK.

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.