webservice学习之cxf框架-spring整合
2016-10-20 13:26
357 查看
1.思路:
Cxf框架本身依赖spring,从官方下载cxf包中有spring的jar包。上边使用JaxWsServerFactoryBean和JaxWsProxyFactoryBean,改为spring配置方式。
发布服务:使用spring和cxf整合的标签<jaxws:server
客户端调用服务:使用spring和cxf整合的标签<jaxws:client
上边的<jaxws:server和<jaxws:client相当于spring容器的bean,发布服务和客户端调用交给spring容器管理。
2.服务端:
2.1创建web工程2.2假如cxf的jar包
2.3开发SEI和实现类:
Cxf开发SEI需要将@webservice注解加在接口中.
SEI接口:
<pre name="code" class="java">@WebService( targetNamespace="http://weather.itcast.cn/",//指定 wsdl的命名空间 name="WeatherInterface",//指定portType的名称 portName="WeatherInterfacePort",//指定port的名称 serviceName="WeatherService"//服务视图的名称 //endpointInterface="cn.itcast.ws.service.WeatherInterface2"//指定哪个接口中方法要发布成webservice服务,接口中加上@webservice注解 ) @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING) public interface WeatherInterface { //查询三天天气 public @WebResult(name="result") List<WeatherModel> queryWeather(@WebParam(name="cityName") String cityName); }
SEI实现类:
public class WeatherInterfaceImpl implements WeatherInterface { @Override public List<WeatherModel> queryWeather(String cityName) { //构造三天天气 List<WeatherModel> list = new ArrayList<WeatherModel>(); Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DATE); WeatherModel weatherModel_1 =new WeatherModel(); weatherModel_1.setDetail("晴"); weatherModel_1.setData(new Date()); weatherModel_1.setTemperature_max(5); weatherModel_1.setTemperature_min(-6); WeatherModel weatherModel_2 =new WeatherModel(); weatherModel_2.setDetail("阴"); calendar.set(Calendar.DATE, day+1); weatherModel_2.setData(calendar.getTime()); weatherModel_2.setTemperature_max(10); weatherModel_2.setTemperature_min(-3); WeatherModel weatherModel_3 =new WeatherModel(); weatherModel_3.setDetail("晴"); calendar.set(Calendar.DATE, day+2); weatherModel_3.setData(calendar.getTime()); weatherModel_3.setTemperature_max(2); weatherModel_3.setTemperature_min(-9); list.add(weatherModel_1); list.add(weatherModel_2); list.add(weatherModel_3); return list; } }
2.4编写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"> <!-- service --> <bean id="weatherInterface" class="cn.itcast.ws.service.WeatherInterfaceImpl"/> <!-- 发布服务 使用jaxws:server和jaxws:endpoint可以发布服务 webservice地址=tomcat地址+cxf servlet的路径+/weather --> <jaxws:server address="/weather" serviceClass="cn.itcast.ws.service.WeatherInterface"> <jaxws:serviceBean> <ref bean="weatherInterface"/> </jaxws:serviceBean> </jaxws:server> </beans>
2.5 在web.xml中配置cxf的servelt ,cxf的servlet用于解析cxf发布webservice
<!-- 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>
2.6 在web.xml中加载spring
<!-- 加载 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>
2.7 启动tomcat测试,输入:http://localhost:8080/ws_1231_cxf_spring_server/ws
2.8 测试注意:
因为通过spring和cxf整合将webservice通过tomcat发布,webservice和应用程序共用一个端口都是8080。
测试webservice和应用程序(jsp)是否可以共存(都可以访问)。
正式上线使用80端口。
3.客户端
3.1使用<jaxws:client调用服务端,<jaxws:client需要cxf和spring整合环境。3.2使用wsdl2java/wsimport工具生成客户端调用代码
3.3编写applicationContext.xml
<span style="font-size:12px;font-weight: normal;"><?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="weatherClient" address="http://localhost:8080/ws_1231_cxf_spring_server/ws/weather?wsdl" serviceClass="cn.itcast.weather.WeatherInterface"> </jaxws:client> </beans></span>
3.4测试
public class ClientTest { private ApplicationContext applicationContext; @Before public void before() { applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); } @Test public void testCxfSpringClient() { // 从spring容器中取出porttype WeatherInterface weatherInterface = (WeatherInterface) applicationContext .getBean("weatherClient"); // 调用portType的方法 List<WeatherModel> list = weatherInterface.queryWeather("郑州"); for (WeatherModel weatherModel : list) { System.out.println(weatherModel.getDetail()); Date date = weatherModel.getData().toGregorianCalendar().getTime(); System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date)); System.out.println(weatherModel.getTemperatureMax()); System.out.println(weatherModel.getTemperatureMin()); } } }
相关文章推荐
- spring和cxf框架整合实现webService服务
- WebService的CXF框架整合Spring
- WebService学习笔记(六)Spring与CXF整合服务端
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- 框架 day64 WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- WebService技术中CXF框架与Spring整合和常见bug
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- WebService(注解),CXF框架(jax-ws,Jax-rs,与spring整合)
- WebService的CXF框架与Spring的整合发布服务入门
- WebService学习之三:spring+cxf整合
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- webservice学习之cxf与spring的整合
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
- Spring整合CXF,发布RSETful 风格WebService-准备工作
- webservice快速入门-使用spring整合apache-cxf(五)
- Spring整合CXF,发布RSETful 风格WebService
- WebService简单例子--cxf整合spring
- Spring整合CXF,发布RSETful 风格-配置WebService
- WebService介绍(WebService基础知识、XFire、CXF(基于XFire和Celtix项目整合而来,与Spring融合好)、AXIS1、AXIS2(有AXIS1转换而来,有Java)
- WebService系列博客{十一}[CXF整合spring实际案例]