当前位置:   article > 正文

创建WebService的几种方式简介(EndPoint、JAX-WS、CXF、axis2、自定义Servlet+Document解析)_webservice的endpoint

webservice的endpoint

一、什么是WebService

        2013年初出校门,便开始了北漂之旅。闲暇时间就看程序方面的书籍,其中李刚老师的《疯狂XML讲义》也曾拜读过,那时便有了WebService的初步认知,Spring+Cxf或Spring+axis2也曾练过,未能在项目上大刀阔斧。直到2016年底,给甲方实施企业服务总线(ESB)的时候 ,开始各种天花乱坠的WebService服务、规范开始应用,也逐步揭开了WebService的层层面纱。那么什么是WebService呢?在这里我仅仅从实用性方面做出解释,更为深层次的不做言谈。

        Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,SOAP是一种WebService实现协议。我们所言的Web Service狭义上就是一种基于SOAP协议的接口规范。那么什么是SOAP协议呢?即HTTP+XML。其定义以HTTP为传输协议,以XML为消息协议(报文规范)的一种通讯、交互、沟通的机制。这种机制实现了系统间的底耦合,只需遵从于协议,可以使用任何代码实现(Tips:上面的定义是一种通俗的解释,并不严谨,比如传输协议HTTP也可能是MQ/JMS等)。

二、WebService的本质是什么

        上面已经初步介绍了什么是WebService,实质就是用一种标准通讯协议(如:HTTP、JMS、MQ),将一个以XML报文为规范的消息传递,这个XML消息规范来源于http://schemas.xmlsoap.org/wsdl/soap/。其将一个XML定义为信封,有报文头Header、报文体Body组成,Header可以有多个Xml元素组成,Body仅仅只能有一个元素组成(多为Complex元素)。

三、Endpoint、JAX-WS、CXF、axis2、自定义Servlet+Document解析,五种方式介绍

       下面我们讲解一个案例,并以此案例采用上述五种方式实现(实质为四种)

案例:某一认证服务,用于认证用户信息,当用户证件号姓名一致,则反馈成功,否则认证失败。同时,为很好的控制服务访问,接入方均需告知接入方标识授权码

案例分析:根据上面案例,我们可以提取出认证的实体(消息),用户(UserInfo)及授权信息(License),用户有UserName、ID两个属性,授权信息有ID及Key两个属性。授权信息通常放置在Header中,便于横向编程访问控制。业务信息放置于Body中,便于具体业务处理。我们确定该服务为认证服务VerifyService,操作为用户认证UserVerify。整体命名空间为:http://hlb.com/verifyService

(一)准备工作

1.编写WSDL如下(如你对XML 命名空间、复杂类型、序列等有所了解,WSDL编写也较为简单):

源文件下载地址:https://download.csdn.net/download/smartyob/12055937

2.java代码反向生成(wsimport其余参数自行研究,不做讲解)

至此,完成WSDL到JAVA代码的转型,编写实现类如下:

准备工作完成

(二)EndPoint、JAX-WS方式的WebService

优点:不依赖于java框架,源于java自身定义

EndPoint发布编写如下:

  1. package test;
  2. import javax.xml.ws.Endpoint;
  3. import com.hlb.verifyservice.impl.VerifyServiceImpl;
  4. public class VerifyServiceMain {
  5. public static void main(String[] args) {
  6. Endpoint endpoint = Endpoint.create(new VerifyServiceImpl());
  7. endpoint.publish("http://127.0.0.1:8090/ws/VerifyService");
  8. }
  9. }

运行发布:

测试成功:

 

JAX-WS方式依赖jar包如下:

主要步骤有:

1.Web.xml申明

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <listener>
  7. <!-- 引入jaxws-ri.jar中的监听器 -->
  8. <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  9. </listener>
  10. <servlet>
  11. <!-- servlet-name与sun-jaxws.xml中的name一致 -->
  12. <servlet-name>VerifyService</servlet-name>
  13. <!-- 使用jaxws-ri.jar中的servlet -->
  14. <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>VerifyService</servlet-name>
  18. <!-- servlet-name与sun-jaxws.xml中的url-pattern一致 -->
  19. <url-pattern>/*</url-pattern>
  20. </servlet-mapping>
  21. </web-app>

2.配置endpoint

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
  3. <endpoint name="VerifyService" implementation="com.hlb.verifyservice.impl.VerifyServiceImpl"
  4. url-pattern="/VerifyService"></endpoint>
  5. </endpoints>

测试结果

VerifyServiceMain 为EndPoint发布,Web启动为JAX-WS发布,程序代码详见:https://download.csdn.net/download/smartyob/12056014

(三)CXF方式的WebService

这里介绍的是Spring方式进行Bean的代理对关键代码贴图如下:

1. web.xml 申明cxf、spring的集成

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <!--配置spring容器的管理 -->
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>classpath:config/spring/applicationContext.xml</param-value>
  10. </context-param>
  11. <listener>
  12. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>
  14. <!-- cxf的配置 -->
  15. <servlet>
  16. <servlet-name>cxf</servlet-name>
  17. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  18. <load-on-startup>1</load-on-startup>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>cxf</servlet-name>
  22. <url-pattern>/ws/*</url-pattern>
  23. </servlet-mapping>
  24. </web-app>

2.applicationContext.xml,spring对Bean的管理

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- Spring 模块化治理及定义 -->
  7. <import resource="spring-cxf.xml" />
  8. <!-- Bean定义 -->
  9. <bean id="VerifyServiceImpl" class="com.hlb.verifyservice.impl.VerifyServiceImpl"></bean>
  10. </beans>

3.spring-cxf.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jaxws="http://cxf.apache.org/jaxws"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://cxf.apache.org/jaxws
  11. http://cxf.apache.org/schemas/jaxws.xsd">
  12. <!-- implementor属性里写的是bean的id,以#,后面是BeanName-->
  13. <jaxws:endpoint implementor="#VerifyServiceImpl" address="/VerifyService" />
  14. </beans>

4.启动测试

程序代码详见:https://download.csdn.net/download/smartyob/12058918

(四)axis2方式的WebService

1. web.xml 申明axis2、spring的集成

 

2.applicationContext.xml,spring对Bean的管理

 

3.启动测试

 

(五)自定义Servlet+Document解析方式的WebService

第二章节已经对WebService有了粗暴的解释,HTTP+XML

HTTP:使用Servlet、SpringMVC的Controller承载

XML:使用Documet解析获取业务数据

数据在哪里呢?答案是 RequestBody中,不做解释,流程如下:

1.创建 WebService的Servlet

  1. package com.hbl.servlet;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.StringReader;
  7. import java.io.StringWriter;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.w3c.dom.Document;
  13. import com.hlb.utils.util.xml.DomUtil;
  14. /**
  15. * Servlet implementation class WebServiceServlet
  16. */
  17. public class WebService extends HttpServlet {
  18. private static final long serialVersionUID = 1L;
  19. /**
  20. * Default constructor.
  21. */
  22. public WebService() {
  23. // TODO Auto-generated constructor stub
  24. }
  25. /**
  26. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  27. */
  28. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  29. response.getWriter().append("Served at: ").append(request.getContextPath());
  30. }
  31. /**
  32. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  33. */
  34. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  35. InputStream in = request.getInputStream();
  36. try {
  37. Document dom = DomUtil.getDocumentByInputStream(in);
  38. String UserName = DomUtil.getNodeByReg(dom.getDocumentElement(), "Body>userVerify>UserInfoData>UserName").getTextContent();
  39. System.out.println("UserName:"+UserName);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. response.getOutputStream().write("<soapenv:Envelope xmlns:soapenv=\\\"http://schemas.xmlsoap.org/soap/envelope/\\\" xmlns:ver=\\\"http://hlb.com/verifyService\\\"><soapenv:Header><ver:License><ver:ID>?</ver:ID><ver:Key>?</ver:Key></ver:License></soapenv:Header><soapenv:Body><ver:UserVerifyResponse><ver:ResponseCode>0000</ver:ResponseCode><ver:ResponseMessage>Suucess</ver:ResponseMessage></ver:UserVerifyResponse></soapenv:Body></soapenv:Envelope>".getBytes());
  44. }
  45. }

2.启动测试

 

1.SOAP报文(XML)HTTP请求获得RequestBody中

2.Dom解析业务数据

3.组织报文放置与ResponseBody中反馈

这个就是本质代码

程序代码详见:https://download.csdn.net/download/smartyob/12061178

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号