理论联系实际,单单只讲理论那就成了纸上谈兵,用一个HelloWorld Demo可来说明事更加直观。那下面咱们就开始进行讲解:
首先到apache官方网下载apache-cxf-2.2.2,地址:http://cxf.apache.org/
新建一个Java Project,导入cxf常用.jar包
- commons-logging-1.1.1.jar
- geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
- geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
- geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
- geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
- geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
- geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
- geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
- jaxb-api-2.1.jar
- jaxb-impl-2.1.12.jar
- jetty-6.1.21.jar
- jetty-util-6.1.21.jar
- neethi-2.0.4.jar
- saaj-api-1.3.jar
- saaj-impl-1.3.2.jar
- wsdl4j-1.6.2.jar
- wstx-asl-3.2.8.jar
- XmlSchema-1.4.5.jar
- xml-resolver-1.2.jar
- cxf-2.2.2.jar
接下就是HelloWorld Demo开发了
第一步:新建一个webservice接口
- @WebService
- public interface IHelloWorld {
- //@WebParam给参数命名,提高可代码可读性。此项可选
- blic String sayHi(@WebParam(name="text") String text);
- }
通过注解@WebService申明为webservice接口
第二步,实现WebService接口
- @WebService
- public class HelloWorldImpl implements IHelloWorld {
- public String sayHi(String name) {
- System.out.println("sayHello is called by " + name);
- return "Hello " + name;
- }
- }
第三步,创建服务端
- public class Server {
- private Server(){
- IHelloWorld helloWorld = new HelloWorldImpl();
- //创建WebService服务工厂
- JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
- //注册WebService接口
- factory.setServiceClass(IHelloWorld.class);
- //发布接口
- factory.setAddress("http://localhost:9000/HelloWorld");
- factory.setServiceBean(helloWorld);
- //创建WebService
- factory.create();
- };
- public static void main(String[] args) throws InterruptedException{
- //启动服务端
- new Server();
- System.out.println("Server ready...");
- //休眠一分钟,便于测试
- Thread.sleep(1000*60);
- System.out.println("Server exit...");
- System.exit(0);
- }
- }
第四步,创建客户端
- public class Client {
- private Client(){};
- public static void main(String[] args){
- //创建WebService客户端代理工厂
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- //注册WebService接口
- factory.setServiceClass(HelloWorld.class);
- //设置WebService地址
- factory.setAddress("http://localhost:9000/HelloWorld");
- IHelloWorld iHelloWorld = (IHelloWorld)factory.create();
- System.out.println("invoke webservice...");
- System.out.println("message context is:"+iHelloWorld.sayHi("
- Josen"));
- System.exit(0);
- }
- }
最后是万事俱备,只欠测试了
首先,运行服务端程序
其次,打开浏览器,在地址栏中输入http://localhost:9000/HelloWorld?wsdl(因为cxf自带了一个jetty服务器),查看接口是否发布成功,如里浏览器页面显示下面内容,证明接口发布成功
- <wsdl:definitions name="IHelloWorldService" targetNamespace="http://client.itdcl.com/">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://client.itdcl.com/">
- <xsd:element name="sayHi" type="tns:sayHi"/>
- <xsd:complexType name="sayHi">
- <xsd:sequence>
- <xsd:element minOccurs="0" name="text" type="xsd:string"/>
- </xsd:sequence>
- </xsd:complexType>
- <xsd:element name="sayHiResponse" type="tns:sayHiResponse"/>
- <xsd:complexType name="sayHiResponse">
- <xsd:sequence>
- <xsd:element minOccurs="0" name="return" type="xsd:string"/>
- </xsd:sequence>
- </xsd:complexType>
- </xsd:schema>
- </wsdl:types>
- <wsdl:message name="sayHi">
- <wsdl:part element="tns:sayHi" name="parameters">
- </wsdl:part>
- </wsdl:message>
- <wsdl:message name="sayHiResponse">
- <wsdl:part element="tns:sayHiResponse" name="parameters">
- </wsdl:part>
- </wsdl:message>
- <wsdl:portType name="IHelloWorld">
- <wsdl:operation name="sayHi">
- <wsdl:input message="tns:sayHi" name="sayHi">
- </wsdl:input>
- <wsdl:output message="tns:sayHiResponse" name="sayHiResponse">
- </wsdl:output>
- </wsdl:operation>
- </wsdl:portType>
- <wsdl:binding name="IHelloWorldServiceSoapBinding" type="tns:IHelloWorld">
- <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHi">
- <soap:operation soapAction="" style="document"/>
- <wsdl:input name="sayHi">
- <soap:body use="literal"/>
- </wsdl:input>
- <wsdl:output name="sayHiResponse">
- <soap:body use="literal"/>
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- <wsdl:service name="IHelloWorldService">
- <wsdl:port binding="tns:IHelloWorldServiceSoapBinding" name="IHelloWorldPort">
- <soap:address location="http://localhost:9000/HelloWorld"/>
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
最后,运行客户端程序,看看效果如果。
这一节就讲到此为止,下节对WSDL定义进行讲解,便于对上面这个Demo进行很好的消化,同时对后面章节启个辅塾作用。
相关推荐
WebService CXF学习——入门篇 1.CXF由来 2.HelloWorld 3.WSDL描述 WebService CXF学习——进阶篇 1.SOAP讲解 2.JAX-WS讲解 3.对象传递 WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF...
使用CXF发布和调用webservice之HelloWorld入门使用CXF发布和调用webservice之HelloWorld入门
cxf写的一个helloworld demo
CXF webservice 入门视频 很清晰很不错,企业当中很多都在使用CXF
NULL 博文链接:https://ryxxlong.iteye.com/blog/1504896
webservice-helloworld 视频 两台机器访问,一个是虚拟机。
webservice-helloworld 视频 两台机器访问,一个是虚拟机。 高清
本示例只是一个cxf的简单的一个helloWorld程序,运行时先运行webServiceApp.java,再在地址栏中输入http://localhost:8080/helloWorld,如果在浏览器中有一串xml输出则成功,然后再运行HelloWorldClient.java文件...
利用CXF3.0+Spring3.2 HelloWorld实例,并且导入了小量的jar包以及在写例子的过程中遇见的一些错误!
webservice地址 http://localhost:8080/springCxf/HelloWorld?wsdl
使用CXF开发简单HelloWorld实例
cxf学习笔记.详细的描述了我学习cxf的过程.服务端采用 cxf+spring的...1:最简单的hello world 2:集合类的传输 3:大数据的传输(上传下载) 4:安全性 4.1:用户令牌 4.2:数字签名 4.3:报文加密 4.4:混合认证
网络服务 一个java webService项目,使用spring / mybatis / apache cxf。 1个数据库配置 ... 我写了一个helloworld文件:src \ main \ java \ com \ neusoft \ webservices \ impl \ TjQueryImpl.java
一篇helloWorld开发说明文档 一篇异常处理文档 一个helloword项目
@Path注解的值是一个相对的URI路径,这个路径指定了该Java类的位置,例如/helloworld。在这个URI中可以包含变量,例如可以获取用户的姓名然后作为参数传入URI中:/helloworld/{username}。 @GET注解是请求方法指示符...
cxf+spring+tomcat 只是演示,一个helloWorld的例子
Cxf自动发布WebService(不使用Spring),包含架包,无需配置,直接访问/cxfService/ws/helloWorld?wsdl
每当收到请求时,helloWorld 服务都会对请求进行一些简单的转换,然后对 exampleGoodbye 进行直接虚拟机调用,并传递有效负载。 exampleGoodbye 服务执行一个简单的转换并将转换后的有效负载返回给 exampleHello,...