700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 使用CXF框架发布SOAP协议的 WebService服务

使用CXF框架发布SOAP协议的 WebService服务

时间:2022-10-19 06:42:50

相关推荐

使用CXF框架发布SOAP协议的 WebService服务

引言

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。特点:可以与Spring无缝连接

步骤

1.使用CXF框架发布webService

官网下载zip包:/download.html解压zip包

2.引入jar包 cxf和spring框架的jar包

<--Spring框架相关jar--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-expression</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.3.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.3.2.RELEASE</version></dependency><--CXF框架相关jar--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.7</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.7</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-core</artifactId><version>3.1.7 </version></dependency>

3 开发服务类接口

@WebServicepublic interface WeatherService {public String queryWeather(String cityName);}

4.开发服务类实现类

@WebServicepublic class WeatherServiceImpl implements WeatherService {@Overridepublic String queryWeather(String cityName) {System.out.println("城市名称:"+cityName);if("北京".equals(cityName)){return "晴转多云~~~";}return "多云转晴....";}}

5.配置spring-cxf.xml 引入新的命名空间cxf

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance" xmlns:jaxws="/jaxws"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /jaxws /schemas/jaxws.xsd"><!--注册服务--><bean id="weatherService" class="com.baizhi.webservice.WeatherServiceImpl"></bean><!--发布服务--><!--address: 代表发布服务的地址 必须以"/"开头serviceClass: 用来书写发布服务的实现类的接口的全名访问服务地址:Http:localhost:8989/项目名/cxf/ws?wsdl--><jaxws:server address="/ws" serviceClass="com.baizhi.webservice.WeatherService"><jaxws:serviceBean><ref bean="weatherService"/></jaxws:serviceBean></jaxws:server></beans>

6.配置web.xml

<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><!--初始化Spring工厂的配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-basic.xml</param-value></context-param><!--启动工厂--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--配置cxf的Servlet 用来区分哪些是SpringMVC的请求,哪些是项目中webService的请求 --><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></web-app>

7.启动服务测试

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。