对于webservice的开发我们一般会使用这3种方法,xfire、axis2、cxf.现在用的比较多的是axis2.但是这种方式还是比较复杂的。即要自己打包程序而且客户端调用起来也比较复杂
还容易出现请求超时的情况而且好多细节问题都比较难以控制。相比之下cxf就比较简单了,它的类可以交给spring进行统一管理。这次就来写一下cxf的使用,还是一步一步的来做一下这个实验。
1.上网下载一个cxf,因为我们要使用到里面的jar包下载地址是http://cxf.apache.org/download.html
2.新建一个动态web项目,拷贝开发包到lib下面
这里就去下载好的cxf中把lib下面的jar包全部复制出来放在项目的lib下面就OK了,这里有很多的包截图只是其中的一部分
3.配置web.xml
<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> <!-- 为spring添加监听器 -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
在web.xml里面加入上面的这一段就OK了
4.编写接口和实现类,这里需要写一个注解名称是WebService
package org.lxh.dao;
import javax.jws.WebService;
@WebService
public interface SayHello {
public String say(@WebParam(name="name")String name);
}
下面编写接口的实现类
package org.lxh.impl;
import org.lxh.dao.SayHello;
public class HelloImpl implements SayHello {
public String say(String name) {
return "你好," + name;
}
}
5.编写服务器端的配置文件(beans.xml),这里要把实现类交给spring管理,后面要配置的就是要发布的webservice
<?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="cxfBean" class="org.lxh.impl.HelloImpl" autowire="byName"></bean>
<jaxws:endpoint id="helloService" implementor="org.lxh.impl.HelloImpl"
address="/HelloWebService"> <!-- implementor后面是实现类的类全名 -->
</jaxws:endpoint>
</beans>
6.服务器端的配置文件写好了下面就是客户端的配置文件(client.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:client id="clientService" serviceClass="org.lxh.dao.SayHello" address="http://localhost/cxf/HelloWebService">
</jaxws:client>
</beans>
6.客户端测试是否能够取得该webservice
package org.lxh.client;
import org.lxh.dao.SayHello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
SayHello helloWorld = (SayHello) context.getBean("cxfBean");
System.out.println(helloWorld.say("Will"));
}
}
下面把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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.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" />
<import resource="beans.xml" />
<import resource="client.xml" />
<bean id="aegisBean" class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
<bean id="jaxws-and-aegis-service-factory" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
<property name="dataBinding" ref="aegisBean" />
<property name="wrapped" value="true" />
</bean>
</beans>
到这里程序就结束了,来看一下运行效果