webService之Apache CXF
来源:互联网 发布:电子商务美工面试校招 编辑:程序博客网 时间:2024/05/17 05:54
webService之Apache CXF
标签:webService CXF
一、CXF简介:
1、Apache CXF是一个开源的webService框架 2、支持多种协议:支持SOAP1.1,SOAP1.2,RESTFUL HTTP,WS-I BasicProfie,WS-Security,等 3、CXF大大简化了WebService的创建且与Spring进行无缝集成。
二、CXF的编程思路
1、编写SEI(服务接口) 2、编写接口实现类 3、配置spring配置文件 4、配置web.xml 5、启动服务器
三、服务端设计
服务接口
/** * 天气服务接口 * @author MOTUI * */@WebServicepublic interface WeatherService { /** * 根据城市名称获得天气情况 * @param cityName * @return */ public String getWeather(String cityName);}
实现类
/** * 服务接口实现类 * @author MOTUI * */@WebServicepublic class WeatherServiceImpl implements WeatherService { public String getWeather(String cityName) { if ("北京".equals(cityName)) { return "阴转多云,18~25摄氏度"; } return "其他地区正在完善"; }}
配置文件(spring 配置文件)
导入命名空间:xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"```xml<!-- 配置bean --> <bean id="weatherService" class="com.motui.service.WeatherServiceImpl"/><!-- 配置webService --><jaxws:server address="/ws" serviceClass="com.motui.service.WeatherService"><jaxws:serviceBean> <ref bean="weatherService"/></jaxws:serviceBean></jaxws:server>```
web.xml
<!-- 加载Spring的工厂监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定监听器寻找Spring配置文件所在位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 配置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>/cxf/*</url-pattern> </servlet-mapping>
启动服务
部署到Tomcat,启动服务。访问路径:http://localhost:port/项目名称/cxf/ws?wsdl进行访问。页面显示wsdl说明书及部署正确。
四、客户端设计
生成客户端代码
使用命令:wsimport -s . -p 包名 wsdl地址
客户端代码(项目为web项目和java项目都可以):
可以直接调用也可以在客户端业务层调用,此处在业务层调用接口代码:
public interface ClientService {public String queryWeather(String cityName);}
实现类代码:(实现类上添加@service注解)
@Servicepublic class ClientServiceImpl implements ClientService { private WeatherService weatherService; public void setWeatherService(WeatherService weatherService) { this.weatherService = weatherService; } @Override public String queryWeather(String cityName) { String weather = weatherService.getWeather(cityName); return weather; }}
测试类代码:
/** * 测试客户端 */public class TestClient { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); //获得ClientService ClientService clientService = (ClientService) context.getBean("clientService"); //调用方法 String queryWeather = clientService.queryWeather("北京"); System.out.println(queryWeather); }}
测试结果:
阴转多云,18~25摄氏度
五、总结
基于CXF与Spring的webService开发。CXF框架与spring可以进行无缝连接。在使用CXF时,需要导入CXF的jar包,在导入全部jar在开发中可能会遇到如下问题:1、配置文件中不自动提示和报红叉问题。 解决方式:联网就可以解决。2、运行过程中报错:prefix wsdp is not bound to a namespace 由以下四个jar导致,删除即可,不删除也不影响正常运行。 cxf-services-ws-discovery-api-3.1.4.jar cxf-services-ws-discovery-service-3.1.4.jar cxf-services-wsn-api-3.1.4.jar cxf-services-wsn-core-3.1.4.jar
0 0
- webService之Apache CXF
- webservice学习之使用Apache CXF和spring搭建webservice
- Apache CXF WebService 使用
- Apache CXF WebService 使用
- apache cxf解析webservice
- Apache CXF实现WebService
- WebService------Apache CXF
- Apache CXF简单WebService例子
- webservice学习-------Apache CXF 简介
- Apache CXF简单WebService例子 .
- Apache CXF简单WebService例子
- Webservice之CXF组件
- webService之cxf
- webservice之cxf
- JAVA webservice之CXF
- webservice之cxf例子
- JAVA webservice之CXF
- CXF 之 Java WebService
- 跟我学Redis(11)—sort排序命令
- Reds3配置文件详解
- 面经
- hdu 1054 Strategic Game 树状DP
- 阿里云服务器初始配置
- webService之Apache CXF
- 移动端的一些常用css的设置 .(2)
- HTML5权威指南+源码资料
- R语言-背景
- 算法导论——15动态规划——15.1钢条切割
- PixHawk折腾日志_APM固件_1_如何新建一种飞行模式
- scala基础39-List伴生对象操作方法
- Android Studio 获取数字签名信息(SHA1)
- CentOS 6.x启动时网卡eth0未激活