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

cxf整合spring实现webservice

2015-01-21 11:25 465 查看
前面一篇文章中,webservice的服务端与客户端都是单独启动,但是在现实项目中,服务端单独启动太没有实际意义了,还是要整合框架启动,所以今天将记录如何整合spring框架。

jar包下载地址如下:
http://yun.baidu.com/share/link?shareid=547689626&uk=2836507213

(一)、web.xml中添加spring与cxf的配置
<?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"> 
<!-- spring配置 -->
<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>
<!-- cxf配置 -->
<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>


(二)、新建接口文件,实现类
接口,需要添加注解webservice
@WebService
public interface IHello {
public String sayHi(String name);

public String printName(String name);
}
实现接口类,指定endpointInterface与serviceName
@WebService(endpointInterface="com.xj.service.IHello",serviceName="hello1Service")
public class HelloImpl implements IHello{

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

@Override
public String printName(String name) {
System.out.println("my name is,"+name);
return "my name is,"+name;
}
}


(三)、配置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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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.xj.service.HelloImpl"/>

<jaxws:endpoint id="testHello" implementor="#hello" address="/testHello" >
</jaxws:endpoint>

</beans>


需要在文件中引入这三个文件,加入相关约束
<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标签,这与直接启动server时,endpoint的publish作用相同,发布这个服务,implementor指定了实现类,这里用的是引用的方式,可以直接如下
<!-- <bean id="hello" class="com.xj.service.HelloImpl"/>  -->
<jaxws:endpoint id="testHello" implementor="com.xj.service.HelloImpl" address="/testHello" >
</jaxws:endpoint>


(四)、项目部署并访问
将项目部署到tomcat中,启动无报错
访问 http://localhost/cxf/testHello?wsdl 可得到我们发布服务的wsdl,其中cxf为项目名称,testHello为我们endpoint中配置的路径

到此为止,服务端就发布完成了。
一般项目中大部分都是单独提供server端接口(即发布服务的wsdl地址),或者根据wsdl地址生成client端。

附加
(五)、启动client端

<?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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<jaxws:client id="hello" serviceClass="com.xj.service.IHello" address="http://localhost/cxf/testHello?wsdl">
</jaxws:client>
</beans>
解释:
jaxws:client 标志为服务的client端,其中address指定了需要访问的wsdl地址,id指定了一个唯一的bean标志。
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextClient.xml");
IHello hello = (IHello) ctx.getBean("hello");
System.out.println(hello.sayHi("xiejun"));

}


如上述,就可以调用服务的相关方法,但是这种调用方法需要项目中含有server端的基础类,如IHello等,显然不太现实。下一篇文章中讲继续记录 如何调用天气预报的接口,现在在那我们是没法知道server端的基础类的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息