Apache CXF之helloworld

1.首先去http://cxf.apache.org/download.html下载CXF所需要的jar,我下载是apache-cxf-2.3.4.zip这个包。

2.建一个java project,并把相关的jar文件添加到classpath路径下.
cxf-2.3.4.jar
jetty-continuation-7.3.1.v20110307.jar
jetty-http-7.3.1.v20110307.jar
jetty-io-7.3.1.v20110307.jar
jetty-server-7.3.1.v20110307.jar
jetty-util-7.3.1.v20110307.jar
servlet-api.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.7.jar


3.创建一个接口

package com.cxf.service;

public interface HelloWorldCxfService {

String sayHello(String username);
}


4.创建该接口的实现类

package com.cxf.service;

public class HelloWorldCxfServiceImpl implements HelloWorldCxfService {

public String sayHello(String username) {
return "Hello,"+username;
}
}


5.发布webservice

package com.cxf.server;

import org.apache.cxf.frontend.ServerFactoryBean;

import com.cxf.service.HelloWorldCxfService;
import com.cxf.service.HelloWorldCxfServiceImpl;

public class Server {

public static void main(String[] args) {
HelloWorldCxfServiceImpl cxfServiceImpl= new HelloWorldCxfServiceImpl();
ServerFactoryBean factoryBean=new ServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
factoryBean.setServiceBean(cxfServiceImpl);
factoryBean.create();
}

}


运行Server,注意不要关闭,在控制台会打印如下信息:

2011-5-11 14:25:30 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
2011-5-11 14:25:30 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/hello
2011-05-11 14:25:30.750:INFO::jetty-7.3.1.v20110307
2011-05-11 14:25:30.859:INFO::Started SelectChannelConnector@localhost:8080
2011-05-11 14:25:30.875:INFO::started o.e.j.s.h.ContextHandler{,null}

6.客户端调用

package com.cxf.server;

import org.apache.cxf.frontend.ClientProxyFactoryBean;

import com.cxf.service.HelloWorldCxfService;

public class Client {

public static void main(String[] args) {
ClientProxyFactoryBean factoryBean=new ClientProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/hello");
factoryBean.setServiceClass(HelloWorldCxfService.class);
HelloWorldCxfService worldCxfService=(HelloWorldCxfService) factoryBean.create();
System.out.println(worldCxfService.sayHello("张三"));
}
}


运行Client代码,控制台打印如下信息:

2011-5-11 14:25:35 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.cxf.com/}HelloWorldCxfService from class com.cxf.service.HelloWorldCxfService
Hello,张三


到此,我们的webservice,已经成功调用了。大家是不是迫不及待的想看下wsdl文件是啥样的呢?
在浏览器中输入http://localhost:8080/hello?wsdl,即可看到wsdl文件了。其中http://localhost:8080/hello部分为代码里指定的Address。
wsdl文件信息:

<wsdl:definitions name="HelloWorldCxfService" targetNamespace="http://service.cxf.com/">

<wsdl:types>

<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://service.cxf.com/">
<xsd:element name="sayHello" type="tns:sayHello"/>

<xsd:complexType name="sayHello">

<xsd:sequence>
<xsd:element minOccurs="0" name="arg0" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

<xsd:complexType name="sayHelloResponse">

<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>

<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>

<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>

<wsdl:portType name="HelloWorldCxfServicePortType">

<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="HelloWorldCxfServiceSoapBinding" type="tns:HelloWorldCxfServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>

<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>

<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="HelloWorldCxfService">

<wsdl:port binding="tns:HelloWorldCxfServiceSoapBinding" name="HelloWorldCxfServicePort">
<soap:address location="http://localhost:8080/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>



最后,附上源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值