spring + cxf 构建 webservice 接口入门
来源:互联网 发布:360软件卸载大师 编辑:程序博客网 时间:2024/05/16 17:10
简单的使用spring、cxf构建webservice
eclipse项目结构,和pom文件(使用maven构建的项目)
pom.xml需要依赖的包
<properties> <cxf.version>2.4.3</cxf.version> <jdk.version>1.6</jdk.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!--CXF --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-management</artifactId><version>${cxf.version}</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version><exclusions><exclusion><artifactId>asm</artifactId><groupId>asm</groupId></exclusion></exclusions></dependency> <dependency><groupId>javax.jws</groupId><artifactId>jsr181-api</artifactId><version>1.0-MR1</version></dependency> </dependencies>
首先定义webservice服务接口:CxfService
package com.test;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface CxfService {@WebMethodpublic String helloWorld(String name);}接口实现类CxfServiceImpl
package com.test;import javax.jws.WebService;@WebService()public class CxfServiceImpl implements CxfService{public String helloWorld(String name) {// TODO Auto-generated method stubreturn "Hello World " + name;}}
测试类TestDemo
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.test.spring.executor.TestService;import com.test.spring.executor.TestTask;public class TestDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");// JaxWsProxyFactoryBean factory = context.getBean(JaxWsProxyFactoryBean.class); //获取客户端实例 CxfService client = (CxfService)context.getBean("cxfServiceClient"); // CxfService client = (CxfService)factory.create(); String respone= client.helloWorld("cyt"); System.out.println(respone); } }
spring相关配置applicationContext.xml(简单起见,服务和客户端放在了同一个项目,要在另一个项目中调用webservice可自行百度)
<?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" /><bean id="cxfService" class="com.test.CxfServiceImpl" /><!-- 发布服务 --><jaxws:endpoint id="cxfServicePublic" implementor="#cxfService"address="/cxfService" /><!-- 调用服务客户端 --><jaxws:client id="cxfServiceClient" serviceClass="com.test.CxfService"address="http://localhost:8080/WebServiceDemo/webservice/cxfService"></jaxws:client></beans>web.xml配置cxf处理webservice服务请求的CXFServlet
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>然后部署到Tomcat服务器
然后运行测试类TestDemo,结果如下:
服务调用成功。
要验证服务是否启动成功可以在浏览器中输入 : http://localhost:8080/WebServiceDemo/webservice/cxfService?wsdl
(格式:http:// + ip + 端口 + 项目名称 + 服务名称 + ?wsdl)
浏览器显示一组xml信息,说明服务启动成功。
初次接触webservice,做下学习笔记,有不妥的地方,还请指出或者有补充的可以下方留言,谢谢!
阅读全文
0 0
- spring + cxf 构建 webservice 接口入门
- spring+cxf调用webservice接口
- webservice:cxf整合spring入门
- XFire+Spring构建WebService接口服务端备忘<入门篇>
- Eclipse+Maven+Spring+CXF 构建webservice 服务
- CXF+spring构建restful风格的webservice
- Eclipse+Maven+Spring+CXF 构建webservice 服务
- CXF集成Spring提供Webservice接口服务
- WebService CXF学习(入门篇5):CXF+Spring+Hibernate
- CXF开发webservice(2):利用spring开发webservice接口
- 采用CXF 构建webservice
- CXF构建webservice解析
- cxf+spring发布webservice接口及接口的调用
- cxf+spring发布webservice接口及接口的调用
- cxf+spring发布webservice接口及接口的调用
- cxf+spring发布webservice接口及接口的调用
- 【WebService框架-CXF】——CXF+Spring+自定义拦截器构建WebService服务端
- 【WebService框架-CXF】——CXF+Spring+Struts+自定义拦截器构建WebService客户端
- IOS开发
- 用python中的list实现用户管理系统(面向对象 CRUD)
- 调和级数(harmony)
- 读书笔记《Inside the C++ Object Model》:关于对象
- Java之IO(一) FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream
- spring + cxf 构建 webservice 接口入门
- python练习(十六)
- 机器学习中的基本数学知识
- vue axios 的使用
- HDU
- java:threadLocal
- 使用.net开发Windows应用程序的过程概要
- Maven搭建SSM框架(详解)
- 接口--代理设计模式