您的位置:首页 > 运维架构 > Apache

spring结合apache cxf发布web service服务

2011-03-15 16:59 330 查看
1. 首先下载apache cxf压缩包,到apache官方网站下载。将lib目录下的jar文件都拷贝到自己的项目lib下面。这里我的项目名为cxfservice
2. 配置sring的配置文件如下:
创建webservice服务接口
package com.cxf.test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
package com.cxf.test;
import javax.jws.WebService;
实现webservice接口,指明服务所实现的接口
@WebService(endpointInterface="com.cxf.test.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
public String sayHello(String text) {
return "hi"+text+"您好";
}
}
3. 配置spring配置文件,配置如下
<?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" />

<bean id="hello" class="com.cxf.test.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/HelloWorld" />
</beans>
4. 到目前为止我们可以启动我们的服务了。然后访问我们的服务http://localhost:8080/cxfservice/HelloWorld?wsdl,会发现报错,这是因为tomcat自带的jar文件和cxf的jar文件冲突。解决方法。在tomcat跟目录下新建文件common\endorsed。将jaxb-api-2.2.1.jar和jaxb-impl-2.2.1.1.jar两个jar文件拷贝到里面去。ok然后启动服务,重新访问一下就会看到生成的wsdl文件了。这时候我们的holleword的webservice服务端就已经创建完了。
5. 如何通过webservice客户端访问我们发布的webservice服务呢。首先创建一个java项目,就叫cxclient,创建接口
package com.cxf.test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String text);
}
6. Spring的配置如下所示:
<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/schema/jaxws.xsd">

<bean id="client" class="com.cxf.test.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.cxf.test.HelloWorld"/>
<property name="address" value="http://localhost:8080/cxfservice/HelloWorld"/>
<!-- 这个地方的地址一定要注意,正确的-->
</bean>

</beans>
7. 好了。最后让我们来测试一下吧。上测试程序。
package com.cxf.test;

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

public class Test {
public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
HelloWorld client = (HelloWorld) ctx.getBean("client");
String result = client.sayHello("你好!");
System.out.println(result);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: