CXF是一个基于 Servlet 技术的 SOA 应用开发框架,简单来说,就是WebService的轻量级实现。
1、下载开发包:http://cxf.apache.org/download.html,选择相应的版本。
2、在eclipse创建maven web工程。
3、打开开发包实例的:java_first_spring_support,将java,resources, webapp复制到工程。
4、复制pom.xml文件到工程,将工程名称修改成自己创建的工程,如:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jaxws</groupId> <artifactId>jaxws</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jaxws Maven Webapp</name> <description>Spring HTTP Sample</description> <packaging>war</packaging> <properties> <cxf.version>${project.version}</cxf.version> <cxf.release.base>${basedir}/../..</cxf.release.base> </properties> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> </plugins> <finalName>JavaFirstSpringSupport</finalName> </build> <profiles> <profile> <id>server</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>demo.spring.service.Server</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>client</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>demo.spring.client.Client</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.16</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.16</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.7.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>8.1.5.v20120716</version> </dependency> </dependencies> </project>
5、完成后目录结构如图:
6、运行项目。
7、配置resouces中client_bean.xml中地址符合你本地的服务,运行Client.java,访问成功。
package demo.spring.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import demo.spring.service.HelloWorld; public final class Client { private Client() { } public static void main(String args[]) throws Exception { // START SNIPPET: client ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client"); String response = client.sayHi("Joe"); System.out.println("Response: " + response); System.exit(0); // END SNIPPET: client } }
时间: 2024-10-13 10:39:35