CXF + Spring + Eclipse simple example, cxfeclipse

Source: Internet
Author: User

[Switch] CXF + Spring + Eclipse concise example, cxfeclipse

Examples of Eclipse + CXF + Spring co-development are for your appreciation.

A good way to interact with multiple systems (heterogeneous systems) is to call Web Services. This example is based on the CXF of Apache, for convenience, it is impossible to write the server and client in the same project, but the client depends on the Web Service interface of the server, so you can export the jar.

Environment:
Eclipse Mars.1 Release (4.5.1)
JDK 1.7.0 _ 15
Tomcat 7
CXF 2.1.3

Spring3

Example project structure:

As shown in, all the dependent third-party libraries are in lib, and all the code is pasted below.

IHelloService. java

Package bing. server; import javax. jws. webService; /*** <p> * WebService interface * </p> ** @ author IceWee * @ date 2012-7-6 * @ version 1.0 */@ WebServicepublic interface IHelloService {public String sayHello (String username );}

HelloServiceImpl. java

Package bing. server; import javax. jws. webService; /*** <p> * WebService implementation class * </p> ** @ author IceWee * @ date 2012-7-6 * @ version 1.0 */@ WebService (endpointInterface = "bing. server. IHelloService ", serviceName =" helloService ") public class HelloServiceImpl implements IHelloService {@ Override public String sayHello (String username) {return" hello, "+ username ;}}

HelloServiceClient. java

Package bing. client; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import bing. server. IHelloService; /*** <p> * WebService caller-client * </p> ** @ author IceWee * @ date 2012-7-6 * @ version 1.0 */public class HelloServiceClient {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext-client.xml"); IHelloService helloService = (IHelloService) context. getBean ("client"); String response = helloService. sayHello ("Peter"); System. out. println (response );}}

ApplicationContext-server.xml

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 = "http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd 6 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 7 <! -- *** Note *** manually added content: xmlns: jaxws = "http://cxf.apache.org/jaxws" http://cxf.apache.org/jaxws 8 http://cxf.apache.org/schemas/jaxws.xsd "--> 9 10 <import resource =" classpath: META-INF/cxf. xml "/> 11 <import resource =" classpath: META-INF/cxf/cxf-extension-soap.xml "/> 12 <import resource =" classpath: META-INF/cxf/cxf-servlet.xml "/> 13 14 <jaxws: endpoint id =" helloService "implementor =" bing. server. helloServiceImpl "15 address ="/helloService "/> 16 17 </beans>

ApplicationContext-client.xml

<? 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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <! -- *** Note *** content manually added: xmlns: jaxws = "http://cxf.apache.org/jaxws" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "--> <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 "/> <bean id =" client "class =" bing. server. IHelloService "factory-bean =" clientFactory "factory-method =" create "/> <bean id =" clientFactory "class =" org. apache. cxf. jaxws. jaxWsProxyFactoryBean "> <property name =" serviceClass "value =" bing. server. IHelloService "/> <property name =" address "value =" http: // localhost: 8082/CXFDemo/ws/helloService "/> </bean> </beans>

Web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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_3_0.xsd">    <display-name>CXFDemo</display-name>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext-server.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>CXFServlet</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>CXFServlet</servlet-name>        <url-pattern>/ws/*</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

All projects have been configured and can be published to Tomcat. Enter http: // localhost: 8082/CXFDemo/ws in the browser and return

The WebService Interface we released is displayed. Click the blue Hyperlink and return

This proves that our Web Service has been released successfully and can be called for testing. Run HelloServiceClient and return

 

The running result using SoapUI is as follows:

 

 

 

 

 

 

Statement: This article reposted from the http://www.blogjava.net/icewee/archive/2012/07/06/382399.html on some of the errors encountered in the problem has been improved, the test can run perfectly

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.