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. FineUI(专业版)v1.2.0 和 FineUI(开源版)v4.1.1 同时发布!

    FineUI(开源版)v4.1.1 (建议所有 v4.x 升级到此版本):http://fineui.com/demo/ +2014-08-15 v4.1.1        -修正Form中表单字段设 ...

  2. LVS持久连接

    LVS持久连接 源地址HASH ipvs的连接模板 可以通过ipvsadm -L -c 持久连接持久客户端连接 PCC:在固定时间内将来自于同一个客户端发往VIP的所有请求统统定向至同一个RS0表示所 ...

  3. 【asp.net】Linux 部署 asp.net core 项目

    Net sdk官网LINUX配置地址:https://www.microsoft.com/net/core#windows 参考:http://www.07net01.com/2016/08/1638 ...

  4. ansible的SSH连接问题

    问题描述: 在ansible安装完毕后一般需要以SSH的方式连接到需要进行管理的目标主机,一开始遇到了如下问题: # ansible -m ping all 10.200.xx.xx | UNREAC ...

  5. bzoj2194: 快速傅立叶之二

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  6. Yii rules常用规则

    public function rules() {     return array(         //必须填写         array('email, username, password, ...

  7. foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'

    错误:foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because ' ...

  8. 第二轮冲刺-Runner站立会议03

    今天做了什么:查看gridview与baseadapter适配器 明天准备做什么:继续gridview与baseadapter适配器 遇到的困难:暂无

  9. Java Web学习笔记3

    今天做了一个实验:Servlet访问WEB-INF目录下的文件notice.html 最后始终不能出现预期的效果,我猜想可能是使用了Tomcat 8版本的原因吧,暂时放下,等以后知识丰富了,再来解决它 ...

  10. JSONP 理解 和 实例 讲解

    1.什么是jsonp 1.1 同源策略 浏览器同源策略的限制,XmlHttpRequest只允许请求当前源(相同域名.协议.端口)的资源. -1)jsonp只支持get请求 -2)不受同源策略限制 , ...