2.CXF:(与spring整合)

CXF相对来说操作没有AXIS繁琐

1.导入spring的jar包和cxf的jar包

2.在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"
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-3.0.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">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!--
通过配置文件发布一个不带接口的webservice
1:id
2:提供服务的访问地址
3:提供服务的类...
-->
<jaxws:endpoint id="springService" address="/springService" implementor="cn.itcast.cxf.web.SpringServiceHello">
</jaxws:endpoint> <!-- 我的天气预报 -->
<!-- <jaxrs: id="weather" address="/weather" serviceClass="cn.itcast.cxf.web.Weather"> </jaxrs:server> -->
<jaxws:endpoint id="weather" address="/weather" implementor="cn.itcast.cxf.web.Weather">
</jaxws:endpoint>
<!-- 音乐人 -->
<jaxws:endpoint id="music" address="/music" implementor="cn.itcast.cxf.web.Music">
</jaxws:endpoint> <!-- 发布一个带接口的webservice
1:id
2:提供服务的访问地址
3:接口的类型
--> <jaxws:server id="ipaddress" address="/ipaddress" serviceClass="cn.itcast.cxf.web.IpAddressService">
<jaxws:serviceBean>
<!-- 接口的实现类... -->
<bean class="cn.itcast.cxf.web.IpAddressServiceImpl"></bean>
</jaxws:serviceBean>
<!-- 请求的消息拦截器 -->
<!-- <jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors> -->
<!-- 响应的消息拦截器... -->
<!-- <jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors> -->
</jaxws:server> </beans> 3.在web.xml中配置cxf的servlet过滤器 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 通过spring 的listener 去解析 cxf-Servlet 配置文件... -->
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> --> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/cxf-servlet.xml</param-value>
</context-param> <!-- 第一次访问CXFServlet 然后/WEB-INF/cxf-servlet.xml 交给 init 方法 去解析...
cxf 与spring 进行无缝的整合,调用spring.jar 包里面的类... 解析 cxf-servlet.xml 配置文件...拿到配置文件里面的信息..
<jaxws:server id="ipaddress" address="/ipaddress" serviceClass="cn.itcast.cxf.web.IpAddressService"> <jaxws:serviceBean>
接口的实现类...
<bean class="cn.itcast.cxf.web.IpAddressServiceImpl"></bean> JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();
request. http://localhost:8080/cxfspringweb/services + /ipaddress bean.setAddress(http://localhost:8080/cxfspringweb/services + /ipaddress); //接口
通过反射拿到cn.itcast.cxf.web.IpAddressService.class
bean.setServiceClass(); bean.ServiceBean(Class.forName("cn.itcast.cxf.web.IpAddressServiceImpl").newInstance()); bean.create(); --> <servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>/WEB-INF/cxf-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
启动服务器,访问wsdl文档。如果访问成功说明接口发布成功 客户端代码生成: 使用jdk命令wsimport -s . -p cn.itcast http://localhost:8080/axis2Server/services/testService?wsdl生成客户端代码,也可将此语句放置在自定义的bat文件中执行 客户端代码调用:(通过配置文件调用) 新建beanssss.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-3.0.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 id="itcast" address="http://192.168.9.12:8080/cxfspringweb/services/ipaddress?wsdl" serviceClass="com.baidu.config.IpAddressService">
</jaxws:client> <jaxws:client id="weather" address="http://192.168.9.12:8080/cxfspringweb/services/weather?wsdl" serviceClass="cn.itcast.weather.Weather">
</jaxws:client> <jaxws:client id="music" address="http://192.168.9.12:8080/cxfspringweb/services/music?wsdl" serviceClass="cn.itcast.music.Music">
</jaxws:client> </beans> 通过bean的方式调用: package cn.itcast.spring.web; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.music.Music;
import cn.itcast.weather.Weather; import com.baidu.config.IpAddressService; /**
*
* 通过配置文件来调用webservice 服务...
* @author Administrator
*
*/
public class SpringConfigInvoke {
/*public Weather weather; public Weather getWeather() {
return weather;
} public void setWeather(Weather weather) {
this.weather = weather;
}*/ /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beanssss.xml");
//根据IP查询所在地
IpAddressService addressService=(IpAddressService) context.getBean("itcast");
String ipaddress=addressService.getAddressByIp("192.168.9.12");
System.out.println(ipaddress); //我的天气
/*SpringConfigInvoke springConfigInvoke = new SpringConfigInvoke();
springConfigInvoke.weather();*/ Weather weather = (Weather) context.getBean("weather");
System.out.println(weather.bjWeather()); //音乐人
Music music = (Music) context.getBean("music");
System.out.println(music.singer());
} /*public void weather(){
weather.bjWeather();
}*/ }

webservice发布服务:CXF及客户端调用的更多相关文章

  1. webservice发布服务:AXIS2及客户端调用

    1.Axis2: 到官网下载axis2的压缩包. 解压后: 1.将lib文件下的jar包复制到项目中 2.在web-inf下创建services->META-INF->services.x ...

  2. spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

    spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...

  3. WebService发布服务例子

    import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface WebServiceI { ...

  4. WebService从服务端到客户端的用例

    1.首先编写Wsdl(基于契约优先的方式),要注意的是命名空间(若是使用include或import)最好使用一致的,代码如下: <?xml version="1.0" en ...

  5. webservice发布问题,部署iis后调用不成功

    我使用的环境win8.vs2010.IIS8.0 vs2010中创建的webservice中的方法可以正常调用,但是发布到IIS上后,asmx文件能正常访问, 但是进入方法后, 点击 "调用 ...

  6. C#开发WEBService服务 C++开发客户端调用WEBService服务

    编写WEBService服务端应用程序并部署 http://blog.csdn.net/u011835515/article/details/47615425 编写调用WEBService的C++客户 ...

  7. 使用soapUI代替WSDL2JAVA生成cxf HTTPS 客户端调用代码

    如果直接用cxf下面的wsdl2java生成https服务调用代码,会报https证书的错误.在你不想导入证书的情况下,可以使用soapUI进行客户端代码的生成,步骤如下: 1.设置CXF,如下图: ...

  8. 搭建基于asp.net的wcf服务,ios客户端调用的实现记录

    一.写wcf 问题: 1.特定的格式 2.数据绑定 3.加密解密 二.发布到iis 问题: 1.访问权限问题,添加everyone权限 访问网站时:http://localhost/WebbUploa ...

  9. CXF 动态创建客户端调用稳定版本号为2.7.18

    今天用动态创建客户端的方式调用webservice,报了这样一个错: 2017-01-05 20:51:46,029 DEBUG main org.apache.cxf.common.logging. ...

随机推荐

  1. knockoutJS学习笔记05:控制文本和外观绑定

    测试数据: function Person(name,age){ var self = this; self.name = ko.observable(name); self.age = ko.obs ...

  2. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  3. js鼠标事件大全

    一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4 Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 onDb ...

  4. loadrunner关联取参--响应值unicode编码处理过

    背景:做电商提交订单,需要获取订单号,然后进行支付.状态变更等操作 submitOrder() { lr_think_time(); /* 提交订单 */ /* specsId:规格ID,hyh_go ...

  5. Java创建对象的几种方法

    有时候,也可能碰到这样面试题,如: Java创建对象有哪几种方法? 除了new之外,java创建对象还有哪几种方式? 本文结合例子,给出几种Java创建对象的方法,Here we go~~~~ 使用n ...

  6. 成为java高手的条件

    世界上并没有成为高手的捷径,但一些基本原则是可以遵循的. 1.扎实的基础 数据结构.离散数学.编译原理,这些是所有计算机科学的基础,如果不掌握它们,很难写出高水平的程序.程序人人都会写,但当你发现写到 ...

  7. 创建面注记PolygonElement

    1.根据4点创建一个面 /// <summary> /// 根据4个点创建图形,点序要顺时针 /// </summary> /// <param name="p ...

  8. cnblog中添加数学公式支持

    在博客中使用数学公式,是一件相对麻烦的事儿,大量的截图和插入图片不仅耗费极大的精力,而且影响写作体验. 虽然对于公式显示已经有多种解决办法,但大多数需要安装插件.而MathML这一雄心勃勃的网页数学语 ...

  9. Django基础,Day9 - 静态文件目录与路径设置说明(eg. images, JavaScript, CSS)

    静态文件路径设置官方说明 1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS. 2. In ...

  10. Robots.txt - 禁止爬虫(转)

    Robots.txt - 禁止爬虫 robots.txt用于禁止网络爬虫访问网站指定目录.robots.txt的格式采用面向行的语法:空行.注释行(以#打头).规则行.规则行的格式为:Field: v ...