您的位置:首页 > 编程语言 > Java开发

spring4.1.6+cxf3.0.8的简单WebService案例(maven工程)

2016-02-18 17:36 721 查看
在学习cxf+spring的时候,网上找了很多的案例,因为版本问题,浪费了大把的时间,如今自己做了个小案例,特将自己的操作过程记录下来,以供大家交流。本人是刚接触cxf,故有许多地方写的不好的,请大神勿喷。

本文中的操作,是在已经安装好eclipse、maven的情况下进行的。

操作环境:

Eclipse: Luna Release (4.4.0)

maven:3.1.1

spring:4.1.6.RELEASE

cxf:3.0.8

jdk:1.6.0_45

tomcat:7.0.39

windows:7(64位)

操作过程:

1 新建maven工程:

右键点击File==》New==》Others,在输入框输入maven,如图,点击next



当出现如下界面时,选择“maven-archetype-webapp”,点击next,



在如下的界面中,填入group Id,artifact Id,点击finish即可。



新建完成后的maven工程,结构如下图:



修改src/main/resources为src/main/java,然后新建src/main/resources(我试过直接新建src/main/java,但是提示已存在,无法新建,具体原因是什么,我没去研究),在src/main/java下新建package,结构如下:



2 修改pom文件,引入所需依赖的jar包;

<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>com.czy</groupId>
<artifactId>cxf.test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cxf.test Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<cxf.version>3.0.8</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
<scope>test</scope>
</dependency>

<!-- spring的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

<!-- cxf的jar包 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ws-metadata_2.0_spec</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
<build>
<finalName>cxf.test</finalName>
</build>
</project>
3 创建webservice接口及实现类:

3.1 接口:

package com.czy.cxf.test;

import javax.jws.WebService;

/**
*  类描述:
*
*  @author:
*
*  History:  2016年2月19日 上午10:27:18      Created.
*
*/
@WebService
public interface IHelloWorld {
public String sayHello();
}
3.2 实现类:

package com.czy.cxf.test;

/**
*  类描述:
*
*  @author:
*
*  History:  2016年2月19日 上午10:41:53     Created.
*
*/
public class HelloWorldImpl implements IHelloWorld {

public String sayHello() {
return "Hello world!";
}

}
4 在src/main/sources下创建spring的配置文件spring-cxf-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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">
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="helloWorld" implementor="com.czy.cxf.test.HelloWorldImpl" address="/HelloWorld"/>
</beans>
<!--
a1c8
END SNIPPET: beans -->
5 修改web.xml文件配置:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-cxf-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>


操作到第5步完成后,项目结构如下:



6 部署、启动项目:
启动成功后,访问http://localhost:8080/cxf.test出现如下界面,表示已经成功:



点击wsdl的链接地址(或直接访问http://localhost:8080/cxf.test/HelloWorld?wsdl),即可查看到相应的wsdl,如下图:



以上就是webservice服务端的实现,接下来要描述的,是客户端的实现。
7  新建client-beans.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/schema/jaxws.xsd">
<bean id="client" class="com.czy.cxf.test.HelloWorldImpl" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.czy.cxf.test.IHelloWorld"/>
<property name="address" value="http://localhost:8080/cxf.test/HelloWorld"/>
</bean>

</beans>
在创建client-beans.xml文件中,要注意"client" bean对应的class是webservice接口的实现类。

8 创建客户端HelloWorldClient:

package com.czy.cxf.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*  类描述:
*
*  @author:
*
*  History:  2016年2月18日 下午2:14:40      Created.
*
*/
public class HelloWorldClient {
private HelloWorldClient() {
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});
IHelloWorld client = (IHelloWorld)context.getBean("client");
String response = client.sayHello();
System.out.println("Response: test  " + response);
System.exit(0);
}
}


备注:ClassPathXmlApplicationContxt(new String[] {"client-beans.xml"}),"client-beans.xml"必须要与新建的client-beans.xml文件名一致,context.getBean("client")中的"client"值必须要与client-beans.xml中的bean
id值一致。
以上步骤结束后,项目结构如下:



9 测试客户端
运行HelloWorldClient,出现以下结果,即表示客户端调用成功。



demo源码下载地址:http://download.csdn.net/detail/u011411602/9436288
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: