加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

CXF 入门实例

发布时间:2020-12-17 01:09:14 所属栏目:安全 来源:网络整理
导读:先去Apache下载cxf的最新jar包? 本人的是 2.3.0 将所有jar加载到类路径下 ? 然后: ---------------------------------------------------------------------- 定义一个接口 ? view plain package ?com.cxf.service.iface;?? ?? import ?javax.jws.WebParam;

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

将所有jar加载到类路径下

?

然后:

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

定义一个接口

?

view plain
  1. package?com.cxf.service.iface;??
  2. ??
  3. import?javax.jws.WebParam;??
  4. import?javax.jws.WebService;??
  5. ??
  6. @WebService??
  7. public?interface?Helloworld?{??
  8. ????public?String?sayHello(@WebParam(name="text")?String?text);??
  9. }??

?

定义一个实现

package?com.cxf.service.impl;??
  • import?javax.jws.WebService;??
  • import?com.cxf.service.iface.Helloworld;??
  • @WebService(serviceName="Helloworld")??
  • 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;??
  • class?HelloworldService?{??
  • ????/**?
  • ?????*?@param?args?
  • ?????*/??
  • 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

    ?

    view plain
      ??<?xml?version="1.0"?encoding="UTF-8"??>???
    1. -?<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">??
    2. ??wsdl:import?location="http://localhost:9000/MyService?wsdl=Helloworld.wsdl"?namespace="http://iface.service.cxf.com/"?/>???
    3. 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">??
    4. />???
    5. wsdl:outputwsdl:operationwsdl:bindingwsdl:service?name="Helloworld"wsdl:port?binding="tns:HelloworldSoapBinding"?name="HelloworldImplPort"soap:address?location="http://localhost:9000/MyService"?wsdl:portwsdl:servicewsdl:definitions>??

    ?

    ?

    抓取有用的信息。。。。

    ?

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

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

    targetNamespace="http://impl.service.cxf.com/

    ?

    ?

    客户端调用

    package?com.cxf.client;??
  • import?javax.xml.namespace.QName;??
  • import?javax.xml.ws.Service;??
  • import?javax.xml.ws.soap.SOAPBinding;??
  • class?HelloworldClient?{??
  • ????//??http://impl.service.cxf.com/??
  • ????private?static?String?namespaceURI?=?"http://iface.service.cxf.com/";???
  • static?QName?SERVICE_NAME?=?new?QName(namespaceURI,?"Helloworld");??
  • static?QName?PORT_NAME?=?"HelloworldPort");??
  • /**?
  • ?????*?@param?args?
  • ?????*/??
  • 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指的是targetNamespace!

    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

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读