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

搭建web项目结合spring+cxf的webservice服务

2014-06-24 16:05 549 查看
[b]服务端:[/b]

服务端和客户端都需要引入包

package cn.itcast.cxf;

public class HiServiceImpl implements IHiService {

@Override
public String sayHi(String name) {
System.out.println("sayHi....");
return "hi " + name;
}

}


View Code
然后

localhost:8080/项目地址/hi?xsdl

[b]客户端[/b]

利用 wsimport -s 地址 或者ws2java -s地址的命令

得到文件后

把接口文件复制来

配置文件 ClientBean.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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 配置客户端bean -->
<!--
id:唯一标识
address:请求的服务地址
serviceClass:客户端接口
-->
<jaxws:client id="hiService" address="http://localhost/CXF_03/cxf/hi" serviceClass="cn.itcast.cxf.IHiService"></jaxws:client>

</beans>


测试 IHiService就是拷贝过来的接口文件,放到项目中

package cn.itcast.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.cxf.IHiService;

public class Test1 {
public static void main(String[] args) {
//初始化spring
ApplicationContext ctx = new ClassPathXmlApplicationContext("ClientBeans.xml");
IHiService s = (IHiService) ctx.getBean("hiService");
s.sayHi("abc");
System.out.println(s.getClass().getName());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: