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

Webservice实战

发布时间:2020-12-16 22:23:41 所属栏目:安全 来源:网络整理
导读:参考链接:http://cxf.apache.org/docs/writing-a-service-with-spring.html 先说下cxf是什么东西吧: Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比

参考链接:http://cxf.apache.org/docs/writing-a-service-with-spring.html

先说下cxf是什么东西吧:

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

?

一、SEI的定义

假设有以下SEI定义:

Java代码 ?

  1. @WebService??
  2. public?interface?OrderProcess?{??
  3. ????public?String?processOrder(Order?order);??
  4. }??

?(实现端省略)

?

二、Server端发布

则最简单的发布Server的方式可以如下:

?

Java代码 ?

  1. Endpoint.publish("http://localhost:8181/orderProcess",?new?OrderProcessImpl());??

?

或者是spring的方式:

Xml代码 ?

  1. <beans?xmlns="http://www.springframework.org/schema/beans"??
  2. ????xmlns:jaxws="http://cxf.apache.org/jaxws"??
  3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:context="http://www.springframework.org/schema/context"??
  4. ????xsi:schemaLocation="??
  5. ???????http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd??
  6. ???????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd??
  7. ???????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context.xsd">??
  8. ??
  9. ????<import?resource="classpath:META-INF/cxf/cxf.xml"?/>??
  10. ????<import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"?/>??
  11. ????<import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?/>??
  12. ??
  13. ????<jaxws:endpoint?id="orderProcess"??
  14. ????????implementor="com.liulutu.liugang.cxf.jaxws.OrderProcessImpl"?address="http://localhost:8090/orderProcess"?/>??
  15. </beans>??

?三、Client端调用

Java代码 ?

  1. Service?service?=?Service.create(new?URL("<wsdlfilepath>"),??
  2. ????????new?QName("namespace",?"servicename"));??
  3. OrderProcess?port?=?orderProcessService.getPort(OrderProcess.class);??
  4. String?s?=?port.processOrder(<arg>);??
  5. System.out.println(s);??

?或者Spring的方式:

Xml代码 ?

  1. <?xml?version="1.0"?encoding="UTF-8"?>??
  2. <beans?xmlns="http://www.springframework.org/schema/beans"??
  3. ???????xmlns:jaxws="http://cxf.apache.org/jaxws"??
  4. ???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  5. ???????xsi:schemaLocation="??
  6. ??????????http://www.springframework.org/schema/beans???
  7. ??????????http://www.springframework.org/schema/beans/spring-beans.xsd??
  8. ??????????http://cxf.apache.org/jaxws???
  9. ??????????http://cxf.apache.org/schemas/jaxws.xsd">??
  10. ???
  11. ????<jaxws:client?id="orderClient"??
  12. ??????????????????serviceClass="com.liulutu.liugang.cxf.codefirst.OrderProcess"??
  13. ??????????????????address="http://localhost:8181/orderProcess"?/>??
  14. </beans>??

?然后在java代码里:

Java代码 ?

  1. ClassPathXmlApplicationContext?xmlApplicationContext?=?new?ClassPathXmlApplicationContext(??
  2. ??????"/META-INF/spring/jaxwsspringclient.xml");??
  3. OrderProcess?bean?=?xmlApplicationContext.getBean(OrderProcess.class);??
  4. System.out.println(bean.processOrder(<order>));?

(编辑:李大同)

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

    推荐文章
      热点阅读