简单cxf+spring构建webservice服务

本文提供了一个使用CXF框架实现Web服务的例子,包括服务端的搭建与客户端调用过程,涉及Spring3.1和CXF2.6版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[/code]  cxf的作用,我就不在这里讲述了,因为网上比我讲的好的多的多的,忽忽!
这篇文章主要是给刚入门cxf的童鞋们一个例子。
我用的jar包是:spring 3.1+ cxf 2.6
首先我们来编写服务端:
step1:Myeclipse 创建web project(project Name: HelloCXF)
实现一个服务:
类Student充当Model:
[code="java"]
@XmlType(name="StudentInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Student implements Serializable{

private static final long serialVersionUID = 6841706329341519463L;

private String id;
private String name;
private Integer age;
private String addr;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
}


定义服务接口:

@WebService
public interface IStudentService {
public void saveStu(@WebParam(name="StudentInfo") Student student,@WebParam(name="saveFlag")boolean flag);
public void updateStu(@WebParam(name="StuInfo")String info);
public void deleteStu(@WebParam(name="id")String id);
}


服务接口实现类:

@WebService
public class StudentServiceImpl implements IStudentService{

public void deleteStu(String id) {
System.out.println("delete Student Id="+id);
}

public void saveStu(Student student, boolean flag) {
System.out.println("save Student !");
}

public void updateStu(String info) {
System.out.println("Update Student Infor!");
}
}


现在服务都写好了,就剩下配置文件了:
配置cxf配置文件:bean.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">
<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" />
<jaxws:endpoint id="stuService" implementor="com.zzn.serviceImpl.StudentServiceImpl" address="/stuService" />
</beans>


配置web.xml


<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/bean.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>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


ok现在可以测试下服务cxf成功没有访问:
http://localhost:8080/HelloCXF/stuService?wsdl

step2:编写客户端程序:
利用cxf的工具wsdl2java生成服务端代码,以便客户端使用:

C:\Users\zzn>wsdl2java -frontend jaxws21 -d e:\ -p com.zzn.service http://localh
ost:8080/HelloCXF/service?wsdl


wsdl2java -frontend的参数作用是为了生成的代码兼容java6,不过不利用这个参数生成的代码会报错(java6的情况下), -d是制定生成代码的路径,-p生成代码的包

在使用这个命令的时候最好要配置环境变量
CXF_HOME=G:\学习\java\webservice\apache-cxf-2.6.0\apache-cxf-2.6.0
PATH中增加:%CXF_HOME%/bin;
CLASSPATH中增加:%CXF_HOME%/lib;

创建java客户端项目(HelloCXFClient)
将上面自动生成的代码放入这个项目。
编写配置文件bean.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-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="service"
address="http://localhost:8080/HelloCXF/stuService"
serviceClass="com.zzn.service.IStudentService" />
</beans>




编写客户端代码:

public class TestClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

IStudentService service = (IStudentService)context.getBean("service");

StudentInfo studentInfo = new StudentInfo();
studentInfo.setAddr("China");
studentInfo.setAge(20);
studentInfo.setId("sd2011022");
studentInfo.setName("zhangzhennan");

service.saveStu(studentInfo, true);
service.deleteStu("sd2011022");
}
}


运行成功!
源代码在附件中,如大家有疑问可以提出!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值