最近由与公司业务的须要,要给一个.net 的系统提供一个webservice接口,以前的框架中是没有集成webservice的,因此就只有本身去搭建了集成了。而后就是在网上各类搜索,都不怎么好使,还好最终搞出来了。java
首先咱们去官网下载apache-cxf-2.7.4,这是我下载的版本,解压后,在目录apache-cxf-2.2.4\lib下面有cxf集成spring 须要的全部jar web
第一步:更具本身项目的状况(主要不要让jar包冲突,有的jar就不须要添加了)添加到本身的项目中;spring
第二步:在项目中写本身须要的发布的接口例如:apache
使用cxf开发webservice这里只须要在接口上加@webservice注解便可app
@WebService @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING) public interface WeatherInterface { //根据城市名称查询将来三天的天气 public List<WeatherModel> queryWeather(String cityName) throws Exception; }
服务接口实现类:使用cxf开发不用在接口实现类上加@webservice注解,由于cxf发布服务时能够指定接口。框架
public class WeatherInterfaceImpl implements WeatherInterface { @Override public List<WeatherModel> queryWeather(String cityName) throws Exception { //构造三天的天气结果 Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DATE); WeatherModel weatherModel_1 = new WeatherModel(); weatherModel_1.setDate(new Date()); weatherModel_1.setDetail("晴朗"); weatherModel_1.setTemperature_max(30); weatherModel_1.setTemperature_min(23); WeatherModel weatherModel_2 = new WeatherModel(); calendar.set(Calendar.DATE, day+1); weatherModel_2.setDate(calendar.getTime()); weatherModel_2.setDetail("晴转多云"); weatherModel_2.setTemperature_max(28); weatherModel_2.setTemperature_min(21); WeatherModel weatherModel_3 = new WeatherModel(); calendar.set(Calendar.DATE, day+2); weatherModel_3.setDate(calendar.getTime()); weatherModel_3.setDetail("多云转小雨"); weatherModel_3.setTemperature_max(25); weatherModel_3.setTemperature_min(18); List<WeatherModel> list = new ArrayList<WeatherModel>(); list.add(weatherModel_1); list.add(weatherModel_2); list.add(weatherModel_3); return list; } }
第三步:配置applicationContext.xmlide
刚开始在网上找得各类文章说要在配置文件中加上下面的配置url
我没有加这几句,加上反而报错,不知缘由下面就是个人applicationContext.xml配置文件spa
<?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服务 --> <jaxws:server address="/weather" serviceClass="cn.itcast.webservice.jaxws.server.WeatherInterface"> <jaxws:serviceBean> <ref bean="weatherInterface"/> </jaxws:serviceBean> </jaxws:server> <!-- 公网天气查询客户端 --> <jaxws:client id="weatherClient" serviceClass="cn.com.webxml.WeatherWebServiceSoap" address="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" /> <!-- 本系统对外服务的天气查询service --> <bean id="weatherInterface" class="cn.itcast.webservice.jaxws.server.WeatherInterfaceImpl" > <property name="weatherWebServiceSoap" ref="weatherClient" /> </bean> </beans>
记得在applicationContext.xml 上面加上.net
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap"
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd;
第四步:配置web.xml 文件
web.xml 文件是web项目的入口,全部的请求最早进入的地方就是他了,在这里spring 的配置我就不写了,只写webservice 的配置(说明你的applicationContext.xml 也是要配置在里面的):
<servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping>