最近项目开发完毕,目前有闲于时间,所以抽空学习了一下,使用cxf开发WebService,废话不多说,直接上码!
背景介绍:
1.开发工具:eclipse
2.jar:cxf+spring,(cxf的包下集成了spring的包,很方便)
步骤如下:
1.创建web项目
2.引入jar,具体引入请查看图片
3.编写对外接口:
@WebService public interface ISayHello { public String sayHello(@WebParam(name="name")String name); }
4.编写接口实现:
@WebService(endpointInterface="com.dao.ISayHello") public class SayHelloImpl implements ISayHello { public String sayHello(String name) { // TODO Auto-generated method stub return "my name is"+name; } }
5.配置spring,创建applicationContext.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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="sayHelloImpl" class="com.dao.impl.SayHelloImpl" /> <jaxws:endpoint id="sayHello" implementor="#sayHelloImpl" address="/sayHello" /> </beans>
6.配置web.xml
<?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" 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>webServiceDemo</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> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/config/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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
自此使用csf 开发webService 已经完毕。
下面我们使用spring注入的方式,编写一个简单客户端,模拟访问我们刚写的webservice.
步骤如下:
1.编写applicationContext_client.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 该方式访问ws服务也是利用spring的依赖注入法 id是spring IOC容器唯一标识符 serviceClass是webservices服务接口 address是服务wsdl地址 --> <jaxws:client id="sayHello2" serviceClass="com.dao.ISayHello" address="http://localhost:8080/webServiceDemo/sayHello?wsdl" /> </beans>
2.client:
public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "com/client/applicationContext_client.xml" }); ISayHello client = (ISayHello) context.getBean("sayHello2"); String response = client.sayHello("Joe"); System.out.println("Response: " + response); System.exit(0); }
3.运行即可,当然首先要启动 我们刚写的webservice服务,不然会提示连接失败!
相关推荐
2.用cxf开发webservice 3.这个服务端和客户端的小demo 在服务端 对外开放接口服务,然后在客户端 调用服务端的方法, 实现客户端(一个javaweb项目)对服务端(javaweb项目)方法的调用, 实际上就是发送和接收消息...
cxf 开发webservice客户端 代码经过测试
CXF开发webservice服务端,希望对大家有所帮助
这是讲解cxf的权威开发文档 文档内容:Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services
03.使用CXF开发WebService客户端
使用jaxws开发webservice。 Webservice三要素 Wsdl(webservice使用说明书)重点掌握 Soap(jaxws开发webservice的传输协议)重点掌握 UDDI(了解) Webservice的使用场景分析(掌握) 学会jaxws基本开发方法...
使用cxf搭建webService,客户端通过接口进行调用
02.CXF功能概述_CXF发展历史和使用CXF开发WebService服务器端
CXF Webservice 开发手册
最近用CXF开发webservice,发现老是报jar包的错误,但是又不能把所有从官网下载下来的都复制过来。官网下载的有好几十个呢,这是官网CXF的下载地址:http://cxf.apache.org/download.html。于是网上整理了一下,做个...
本java工程实现了spring+cxf快速开发webservice jar包齐全导入myeclipse可直接二次开发
cxf开发webservice与spring整合所需jar包
傻瓜试记录开发步骤,具体实现自己去完成。 如果看不懂,找自已原因。
java语言实现使用spring+mybatis+oracle访问数据库,使用cxf发布webservice,并使用axis调用webservice。项目使用maven管理jar包。
使用CXF开发简单HelloWorld实例
NULL 博文链接:https://jin8000608172.iteye.com/blog/1768378
cxf 开发webservice与调用demo
cxf开发webservice.docxcxf开发webservice.docx
在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式(或binding)和网络协议(transports)包括SOAP、XML(通过HTTP或JMS)进行访问。CXF同样支持多种model 如:JAX-WS,JBI,SCA和CORBA service。CXF设计...