首先是服务器端:

//实体类
public class Weather { private String region;//区域编码
private String regionName;//区域名称
private float temperature;//温度
private boolean isRain;//是否下雨 public Weather() {
super();
}
public Weather(String region, String regionName, float temperature,
boolean isRain) {
super();
this.region = region;
this.regionName = regionName;
this.temperature = temperature;
this.isRain = isRain;
} public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getRegionName() {
return regionName;
}
public void setRegionName(String regionName) {
this.regionName = regionName;
}
public float getTemperature() {
return temperature;
}
public void setTemperature(float temperature) {
this.temperature = temperature;
}
public boolean isRain() {
return isRain;
}
public void setRain(boolean isRain) {
this.isRain = isRain;
} @Override
public String toString() {
return "Weather [region=" + region + ", regionName=" + regionName
+ ", temperature=" + temperature + ", isRain=" + isRain + "]";
}
}
//SEI
@WebService
public interface WeatherDao { //通过区域编码查找该地区天气情况
@WebMethod
Weather findWeatherByRegion(String region);
}
//SEI实现类
@WebService
public class WeatherService implements WeatherDao { @Override
public Weather findWeatherByRegion(String region) {
return new Weather("fjxm", "福建省厦门市", 20, true);
} }
//配置beans.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- cxf的一些核心配置(必须引入) -->
<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" /> <!-- implementor:SEI实现类全类名 -->
<!--
address:名字可以任意取;
webService部署路径:主机名/工程名/address
wsdl文档:通过主机名/工程名/address?wsdl
不再需要手动去发布webService!
-->
<jaxws:endpoint id="weatherWS"
implementor="com.cxf.service.WeatherService"
address="/weatherws" />
</beans>
//配置web.xml,spring文件随容器启动加载
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>cxf_spring_webService_server</display-name>
<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>
<!-- 配置beans.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
//发布该工程,查看wsdl文档:

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://dao.cxf.com/" name="WeatherServiceService" targetNamespace="http://service.cxf.com/">
<wsdl:import location="http://localhost:8080/cxf_spring_webService_server/weatherws?wsdl=WeatherDao.wsdl" namespace="http://dao.cxf.com/">
</wsdl:import>
<wsdl:binding name="WeatherServiceServiceSoapBinding" type="ns1:WeatherDao">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="findWeatherByRegion">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="findWeatherByRegion">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="findWeatherByRegionResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WeatherServiceService">
<wsdl:port binding="tns:WeatherServiceServiceSoapBinding" name="WeatherServicePort">
<soap:address location="http://localhost:8080/cxf_spring_webService_server/weatherws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

************至此,服务器端发布成功,接下来是客户端生成代码以及配置和测试****************

//配置客户端的client-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"> <!-- serviceClass:SEI -->
<!-- address:server端webService的发布地址 -->
<jaxws:client id="weatherClient"
serviceClass="com.cxf.dao.WeatherDao"
address="http://localhost:8080/cxf_spring_webService_server/weatherws" />
</beans>
//生成客户端代码同cxf生成客户端代码步骤---略
//测试
public class Test { public static void main(String[] args) {
//获取spring容器,自动创建WeatherDao实现类对象
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("client-beans.xml"); //client-beans.xml里的id
WeatherDao weatherDao = (WeatherDao) context.getBean("weatherClient");
Weather weather = weatherDao.findWeatherByRegion("fjxm");
System.out.println(weather);
}
}

执行结果:

Weather [rain=true, region=fjxm, regionName=福建省厦门市, temperature=20.0]

6.用CXF编写基于Spring的WebService的更多相关文章

  1. Spring整合CXF步骤,Spring实现webService,spring整合WebService

    Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...

  2. 7.添加基于Spring的WebService拦截器

    客户端拦截器: public class AccountInterceptor extends AbstractPhaseInterceptor<SoapMessage>{ private ...

  3. 使用CXF实现基于Rest方式的WebService(转)

    转自:https://www.cnblogs.com/zjm701/p/6845813.html原文更清晰 本文介绍使用CXF实现基于Rest方式的WebService(CXF的版本是3.0.0) 一 ...

  4. 使用CXF实现基于Rest方式的WebService

    本文介绍使用CXF实现基于Rest方式的WebService(CXF的版本是3.0.0) 一. 前言 Java有三种WebService规范:Jax-WS,Jax-RS,Jaxm 1. Jax-WS( ...

  5. 使用CXF实现基于Soap协议的WebService

    本文介绍使用CXF实现基于Soap协议的WebService(CXF的版本是3.0.0) 一. 前言 Java有三种WebService规范:Jax-WS,Jax-RS,Jaxm 1. Jax-WS( ...

  6. Spring整合CXF,发布RSETful 风格WebService(转)

    Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...

  7. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  8. Spring整合CXF,发布RSETful 风格WebService

    原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...

  9. CXF+Spring搭建webservice服务

    Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS .这些 Services 可以支持多 ...

随机推荐

  1. 测试webAPP时图标背景一片黑色

    写了个web小程序,打包成APP后安装到手机里,在魅族手机里显示是正常的,但是在EMUI和MIUI中却显示一片黑色,搞了半天才发现原来是因为EMUI会自动给应用的图标裁剪圆角并加上背景色,背景色是系统 ...

  2. c#.net全站防止SQL注入类的代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  3. MSSql Server 索引'uq_f_userName' 依赖于 列'f_userName'。由于一个或多个对象访问此列,ALTER TABLE ALTER COLUMN f_userName 失败

    --需求有变,需要往t_login表的f_userName字段添加外国人名,之前设置的varchar(10)不够,商议决定改成varchar(30),执行的时候,提示消息 索引'uq_f_userNa ...

  4. github简单使用教程

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...

  5. spring 多线程 注入 服务层 问题

    在用多线程的时候,里面要用到Spring注入服务层,或者是逻辑层的时候,一般是注入不进去的.具体原因应该是线程启动时没有用到Spring实例不池.所以注入的变量值都为null. 详细:http://h ...

  6. 第八章 springboot + mybatis + 多数据源(转载)

    本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5413845.html 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构 ...

  7. Knockout.Js官网学习(value绑定)

    前言 value绑定是关联DOM元素的值到view model的属性上.主要是用在表单控件<input>,<select>和<textarea>上. 当用户编辑表单 ...

  8. Daily Scrum 12.15

    今日完成任务: 完成关于主页右侧资源显示的算法优化:解决了下载资源时的异常. 遇到困难: 编译课设这周要检查,小组成员的大部分时间在完成编译课设,时间很紧. 明日任务: 孙思权 完成第二组提供的数据库 ...

  9. 20145202、20145225、20145234 《信息安全系统设计基础》实验五 简单嵌入式WEB 服务器实验

    实验内容 1.配置环境 2.使用vi 编辑器阅读理解源码 2.编译应用程序 运行 make 产生可执行文件httpd 3.下载调试 使用 NFS 服务方式将HTTPD 下载到开发板上,并拷贝测试用的网 ...

  10. JSTL和EL

    JSTL和EL 1.EL表达式总是放在大括号里,而且前面有一个美元符前缀. person.name2.相似点:(1).都可以让我们在静态内蓉中插入动态信息.EL:室外温度是{emp}度 JSP:室外温 ...