CXF也就是用于发布接口的webservice的框架。
发布服务:使用spring和cxf整合的标签<jaxws:server
客户端发布服务:使用springcxf整合的标签<jaxws:client
上边的两个标签就是spring里面的bean,发布服务和客户端调用就交给spring来管理。
1. 首先创建一个web工程,将CXF的jar添加进去,在CXF的jar中存在spring的jar,本人使用的是CXF2.7.10;
自带的Spring是3.0;
2. 开发SEI(接口)还有实现类;
使用webservice的方式(就是不用CXF) @webservice
注解是写在实现类上,而使用CXF注解是写在接口上的
(SEI)接口代码:/**
* 用于查询天气的接口
*
* @return
*/
@WebService(targetNamespace = "http://weatherCXF.cn", // 指定WSDL的命名空间
name = "weatherCXF", // 指定porttype的name;
portName = "weatherCXFPort", // 指定port的名称
serviceName = "weatherService"// 指定service的name
)
public interface ServerSei {
public @WebResult(name = "result")
String getWeatherByCity(@WebParam(name = "cityName") String cityName);
}
实现类: (比较简洁。。实现功能)
public class ServerSeiImp implements ServerSei {
public String getWeatherByCity( String cityName) {
return cityName + " 是晴天";
}
}
3.编写applicationContext.xml配置文件(也就是spring的配置文件);
文件中的约束要包括CXF的,还有cxf和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">
<bean id="SeiImp" class="com.server.test.ServerSeiImp"></bean>
<!-- jaxws:server
和使用 jaxws:endpoint 是一样的效果
webservice的地址:tomcat的地址+cxf servl的路径+/weather
-->
<jaxws:server address="/weather" serviceClass="com.server.test.ServerSei" >
<jaxws:serviceBean >
<ref bean="SeiImp"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
4.添加CXF的servlet配置 并且 加载配置文件
<!-- 配置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>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- 加载spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
5.启动tomcat
6.访问发布的服务
http://localhost:8080/CxfAndSpring/ws/weather?wsdl
OK 成功~ 明天再写cxf_spring_client