关于cxf的webservice搭建

本文详细介绍如何使用CXF搭建WebService服务端及客户端。包括接口定义、实现类、Spring配置、Jar包导入、web.xml设置等步骤,并提供测试示例。

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

WebService的实现方式有很多,这里主要介绍关于cxf的WebService的搭建。

参考连接:http://boyadn.blog.163.com/blog/static/74230736201322104510611/

1.首先是搭建服务端

我这里服务端主要包括两个类,一个是接口和一个是实现类。

[img]http://dl2.iteye.com/upload/attachment/0101/9530/6f32cdf1-0593-3424-8b5f-30f5a22fb916.png[/img]

Hello.java的具体代码如下:
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface Hello {
@WebMethod
public String sayHello(@WebParam(name="name") String name);
}
代码很简单,只有一个方法sayHello。

实现类HelloImpl.java如下:
import javax.jws.WebService;
import com.lml.ws.service.Hello;

@WebService(endpointInterface = "com.lml.ws.service.Hello")
public class HelloImpl implements Hello {

public String sayHello(String name) {
return name + " say hello!";
}
}
主要是对Hello接口的实现。

因为使用了spring来管理,所以这里需要配置applicationContext,applicationContext.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="helloService"
implementor="com.lml.ws.service.Impl.HelloImpl"
address="/HelloService" />
</beans>

因为有使用spring 和 cxf,所以先导入相关jar包,我导入的jar包如下:

[img]http://dl2.iteye.com/upload/attachment/0101/9534/d508af9b-eeee-3548-b50d-97a62e0c32dc.png[/img]

具体的jar包和项目下载地址:http://download.youkuaiyun.com/detail/l540151663/8036439

最后在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>classpath:applicationContext.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>
</web-app>

到此,服务端搭建完毕。

2.客户端的搭建WebClient
建立java项目后,在项目中导入相关jar包,如下:

[img]http://dl2.iteye.com/upload/attachment/0101/9538/58bf8e1c-8b9c-3f6f-9c48-77d7b1903c77.png[/img]

服务端放入容器tomcat中,然后开启,用url访问页面http://localhost:8080/WebService/HelloService?wsdl
访问内容如下:

[img]http://dl2.iteye.com/upload/attachment/0101/9558/5caa6fb9-2a48-3834-a26e-1781eb16ff9d.png[/img]
将该页面另存为HelloService.xml,这里要注意,xml文件是没有样式的。

因为需要引用到服务端的Hello实例,所以这里需要做些处理。使用wsdl2java生成客户端代码。这里需要用到一个包apache-cxf-3.0.1。解压后在C:\Users\Administrator\Desktop\apache-cxf-3.0.1\apache-cxf-3.0.1\bin下有wsdl2java文件。打开cmd,进入该bin目录下,执行命令:C:\Users\Administrator\Desktop\apache-cxf-3.0.1\apache-cxf-3.0.1\bin>wsdl2java -
p com.lml.ws.client -d d:\workspace\WebClient\src -verbose "C:\Users\Administrat
or\Desktop\HelloService.xml"
关于上面的命令解析:
wsdl2java -p 包名 -d 生成文件路径 -verbose "wsdl文件路径"
包名:文件存放的包名,可以写项目中包路径
生成文件路径 :文件存放路径,可以直接写项目路径
wsdl文件:在IE中执行服务端URL显示的XML另存为XML文件。

[img]http://dl2.iteye.com/upload/attachment/0101/9574/b935dd5f-8b11-3040-924c-ca17b066f233.png[/img]


执行后刷新WebClient项目,生成内容如下图:

[img]http://dl2.iteye.com/upload/attachment/0101/9570/f7f00611-70f0-31e3-9224-3e9a1c452e4e.png[/img]

最后在客户端进行测试,Test.java代码如下:
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.lml.ws.client.Hello;

public class Test {
public static void main(String[] args) {
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(Hello.class);
//设置WebService地址
factory.setAddress("http://localhost:8080/WebService/HelloService");
Hello helloService = (Hello)factory.create();
System.out.println("message context is:"+helloService.sayHello("lml"));
}
}

执行代码,结果如下图:

[img]http://dl2.iteye.com/upload/attachment/0101/9590/a11ebc8d-7ee6-3a20-9a92-357b56526302.png[/img]

具体代码和使用工具请到上面地址下载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值