CXF 入门实例

先去Apache下载cxf的最新jar包 本人的是 2.3.0

将所有jar加载到类路径下

然后:

----------------------------------------------------------------------

定义一个接口

package com.cxf.service.iface; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface Helloworld { public String sayHello(@WebParam(name="text") String text); }

定义一个实现

package com.cxf.service.impl; import javax.jws.WebService; import com.cxf.service.iface.Helloworld; @WebService(serviceName="Helloworld") public class HelloworldImpl implements Helloworld { public String sayHello(String text) { return "hello " + text; } }

第三步: 发布web service 服务

package com.cxf.service.app; import javax.xml.ws.Endpoint; import com.cxf.service.impl.HelloworldImpl; public class HelloworldService { /** * @param args */ public static void main(String[] args) { HelloworldImpl implementor = new HelloworldImpl(); String address = "http://localhost:9000/MyService"; Endpoint.publish(address, implementor); System.out.println("服务已经发布...."); } }

打开浏览器 输入http://localhost:9000/MyService?wsdl

<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions name="Helloworld" targetNamespace="http://impl.service.cxf.com/" xmlns:ns1="http://iface.service.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:import location="http://localhost:9000/MyService?wsdl=Helloworld.wsdl" namespace="http://iface.service.cxf.com/" /> - <wsdl:binding name="HelloworldSoapBinding" type="ns1:Helloworld"> <soap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="sayHello"> <soap:operation soapAction="" style="document" mce_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="Helloworld"> - <wsdl:port binding="tns:HelloworldSoapBinding" name="HelloworldImplPort"> <soap:address location="http://localhost:9000/MyService" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

抓取有用的信息。。。。

wsdl:definitions name="Helloworld".....

<wsdl:import location="http://localhost:9000/MyService?wsdl=Helloworld.wsdl" namespace="http://iface.service.cxf.com/" />

客户端调用

package com.cxf.client; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.soap.SOAPBinding; import com.cxf.service.iface.Helloworld; public class HelloworldClient { // http://impl.service.cxf.com/ private static String namespaceURI = "http://iface.service.cxf.com/"; private static QName SERVICE_NAME = new QName(namespaceURI, "Helloworld"); private static QName PORT_NAME = new QName(namespaceURI,"HelloworldPort"); /** * @param args */ public static void main(String[] args) { Service service = Service.create(SERVICE_NAME); String endpointAddress = "http://localhost:9000/MyService"; service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); Helloworld hw = service.getPort(Helloworld.class); String info = hw.sayHello(""); System.out.println(info); } }

注意几点:

1:namespaceURI

2:PORT_NAME 的第二个参数 是 服务名 + port

而不是<wsdl:port binding="tns:HelloworldSoapBinding" name="HelloworldImplPort">
中的 HelloworldImplPort 这点不是很懂。 先记着就ok

3: 如果namespaceURI SERVICE_NAME PORT_NAME 配置错误, 将会抛出一个

Invalid address. Endpoint address cannot be null.

另外 namespaceURI 末尾有一个反斜杠 /

4:endpointAddress 区分大小写 。 否则将会抛出一个404

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值