WebService的CXF框架与Spring的整合发布服务入门
来源:互联网 发布:淘宝店铺首页产品链接 编辑:程序博客网 时间:2024/06/05 19:27
1、创建WEB项目,将CXF与SPRING的包引入项目(在下载的CXF的lib包里,里面已经整合好了,直接引入进去就行了)
2、创建接口
package com.ckinghan.cxf.server.service;import javax.jws.WebService;import javax.xml.ws.BindingType;import javax.xml.ws.soap.SOAPBinding;/** * 使用CXF框架,在接口上要使用@WebService注解 * @author Ckinghan */@WebService//这里指定创建的是SOAP1.2的,如果不指定,创建的是SOAP1.1的,现在大部分使用的都是1.2的@BindingType(SOAPBinding.SOAP12HTTP_BINDING)public interface WeatherService { public String getWeather(String cityName);}
3、创建接口的实现类
package com.ckinghan.cxf.server.service.Impl;import com.ckinghan.cxf.server.service.WeatherService;public class WeatherServiceImpl implements WeatherService { @Override public String getWeather(String cityName) { System.out.println("收到来自客户端面的请求:"+cityName); return "这是一个好天气"; }}
4、在项目中创建config (Source Folder)文件夹,并在此文件中创建applicationContent.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"> <!-- spring对Endpoint也做了封装,可以使用jaxws:endpoint进行服务发布 --> <jaxws:endpoint address="/hello" implementor="com.ckinghan.cxf.server.service.test.EndpointTest"/> <!-- 配置WebService的发布服务,设置访问地址(默认的已经有地址,只需要将访问的最后路径添加上即可),设置访问接口 --> <jaxws:server address="/weather" serviceClass="com.ckinghan.cxf.server.service.WeatherService"> <!-- 引入接口的实现类 --> <jaxws:serviceBean> <ref bean="serviceBean" /> </jaxws:serviceBean> <!-- 配置拦截器 --> <jaxws:inInterceptors > <ref bean="inInterceptor"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <ref bean="outInterceptor"/> </jaxws:outInterceptors> </jaxws:server> <!-- 设置接口的实现类 --> <bean name="serviceBean" class="com.ckinghan.cxf.server.service.Impl.WeatherServiceImpl"/> <!-- 配置拦截器 --> <bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </beans>
5、配置web.xml文件,
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>cxfServer1</display-name> <!-- 设置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> </servlet> <servlet-mapping> <servlet-name>CXF</servlet-name> <!-- 注意这里的路径 --> <url-pattern>/webService/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
6、创建测试Sping 封装Endpoint发布服务的文件
package com.ckinghan.cxf.server.service.test;import javax.jws.WebService;@WebServicepublic class EndpointTest { public static void main(String[] args) { System.out.println("HELLO"); }}
7、启动项目
8、通过http://localhost:8080/cxfServer1/webService/weather?wsdl访问项目是否部署成功,如果成功会返回以下界面
9、通过http://localhost:8080/cxfServer1/webService/hello?wsdl访问使用发布的服务是否成功,如果成功,会出现 以下界面:
10、创建客户端的JAVA项目,并使用 wsdl2java -p com.ckinghan.cxf.client -d 项目目录 http://localhost:8080/cxfServer1/webService/weather?wsdl生成客户端代码
11、引入JAR包
12、创建config Source Folder,并创建applicationContext.xml可以从服务层COPY过来修改
<?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"> <!-- 配置WebService客户端,设置访问地址(这里需要填写全路径),设置访问接口 ,定义ID用以获取--> <jaxws:client id="weatherClient" serviceClass="com.ckinghan.cxf.server.service.WeatherService" address="http://localhost:8080/cxfServer1/webService/weather"/></beans>
13、创建测试 类
package com.ckinghan.cxf.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.ckinghan.cxf.server.service.WeatherService;public class CXFClientTest { public static void main(String[] args) { //获取Spring上下文 ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); //获取bean对象 WeatherService weatherService = (WeatherService) context.getBean("weatherClient"); //调用接口法,获取信息 String weather = weatherService.getWeather("上海"); //输出获取的信息 System.out.println(weather); }}
14、执行。输出结果如下:
阅读全文
0 0
- WebService的CXF框架与Spring的整合发布服务入门
- webservice--CXF+Spring整合发布SOAP协议的服务
- webservice--CXF+Spring整合发布REST的服务
- 8、CXF与Spring整合发布http rest 风格的WebService服务
- WebService之CXF整合Spring框架发布REST服务
- 7、CXF与Spring整合,发布WebService服务
- WebService的CXF框架整合Spring
- (四)CXF整合Spring发布WebService服务
- Spring整合CXF之发布WebService服务
- Spring整合CXF之发布WebService服务
- Spring整合CXF之发布WebService服务
- Spring整合CXF配置WebService服务发布
- Spring整合CXF之发布WebService服务
- WebService cxf 与 ssm框架的整合
- WebService之CXF与Spring的整合
- 【webservice】CXF结合spring发布简单的webservice服务
- CXF与Spring整合开发WebService服务
- spring boot整合cxf发布webservice服务和spring boot整合cxf客户端调用webservice服务
- 计算机专业清华世界第一!US NEWS全球大学排名出炉
- 苹果音箱HomePod体验:除了亮灯,现在啥都做不了 | WWDC 2017
- WWDC 2017上,除了iOS 11还有什么值得我们期待?
- 想象一下,除了Siri,iOS 11还会给iPhone带来哪些惊喜?
- 苹果iOS 11要对Siri 大升级:扩大第三方应用访问范围
- WebService的CXF框架与Spring的整合发布服务入门
- 使用fetch代替传统的Ajax
- POI导出#JQuery+SpringMVC+POI
- 一键安装python3.5->virtualenv->django开发环境
- int long unsigned 取值范围
- drbd+MFS+pacemaker+rocosync的报错问题
- 腾讯CTF首日赛况焦灼,俄罗斯安全战队暂居榜首
- 除了A10X处理器,5188的起售价,新一代iPad Pro还有什么看点?
- 一文看完:18核iMac、神秘Siri音箱......这是苹果最棒一届WWDC