准备:
myeclipse apache-cxf-3.2.2
服务端:
1.创建一个web工程
2.将apache-cxf-3.2.2下lib下的包复制到lib下
3.创建服务接口及服务类
服务接口(@WebService-定义服务):
使用cxf开发webservice这里只需要在接口上加@webservice注解即可,和jaxws开发不同。
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String sityName);
}
4.服务接口实现类(自己实现想要实现的功能):
使用cxf开发不用在接口实现类上加@webservice注解,因为cxf发布服务时可以指定接口
public class WeatherfaceImpl implements WeatherInterface {
public String queryWeather(String sityName) {
Calendar calendar=Calendar.getInstance();
int day=calendar.get(Calendar.DATE);
return day+"多云";
}
}
5.创建application.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- service -->
<bean id="weatherInterface" class="com.service.WeatherfaceImpl"></bean>
<!--
发布服务
使用jaxws:server和jaxws:endpoint可以发布服务
WebService地址=Tomcat地址值+CXF Servlet的路径+ /weather
-->
<jaxws:server address="/weather" serviceClass="com.service.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
另:配置文件的配置有多种方法,这里的配置为简洁版
6.在web.xml中加入配置:
<!-- 加载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>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 本系统的WebService路径必须以/ws/开头 -->
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
7.部署项目 启动tomcat, 在浏览器中输入:http://localhost:8080/WebService_CXF_Spring_Server/ws,如图所示:
点击即为WSDL地址:
工程结构如下:
客户端:
使用wsdl2java命令将WSDL生成客户端代码:
1.配置环境变量(apache-cxf-3.3.2路径):
在path的末尾添加:%CXF_HOME%\bin;
跳转到wsdl2java所在的位置执行命令:
生成的代码就在该目录下,将代码拷贝到工程中
2.新建配置文件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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!--
使用<jaxws:client>调用服务端
jaxws:client内部使用JaxWsProxyFactoryBean方式
serviceClass:指定portType地址(需要使用wsdl2java工具生成)
-->
<jaxws:client id="weathClient" address="http://localhost:8080/WebService_CXF_Spring_Server/ws/weather?wsdl"
serviceClass="com.service.WeatherInterface"/>
</beans>
创建测试类:
public class ClientTest {
private static ApplicationContext applicationContext;
public static void main(String[] args){
applicationContext=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//从Spring容器中取出portType
WeatherInterface weatherInterface=(WeatherInterface) applicationContext.getBean("weathClient");
System.out.println(weatherInterface.queryWeather("海南"));
}
}
运行结果:
工程结构如下: