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

1 篇文章 0 订阅
1 篇文章 0 订阅

一、什么是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发布编写如下:

package test;

import javax.xml.ws.Endpoint;

import com.hlb.verifyservice.impl.VerifyServiceImpl;

public class VerifyServiceMain {
	public static void main(String[] args) {
		
		Endpoint endpoint = Endpoint.create(new VerifyServiceImpl());
		endpoint.publish("http://127.0.0.1:8090/ws/VerifyService");
		
		
	}
}

运行发布:

测试成功:

 

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

主要步骤有:

1.Web.xml申明

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	
	<listener>
        <!-- 引入jaxws-ri.jar中的监听器 -->
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <!-- servlet-name与sun-jaxws.xml中的name一致 -->
        <servlet-name>VerifyService</servlet-name>
        <!-- 使用jaxws-ri.jar中的servlet -->
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>VerifyService</servlet-name>
        <!-- servlet-name与sun-jaxws.xml中的url-pattern一致 -->
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    
</web-app>

2.配置endpoint

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

测试结果

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

(三)CXF方式的WebService

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

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<!--配置spring容器的管理 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/spring/applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- cxf的配置 -->
	<servlet>
		<servlet-name>cxf</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>
</web-app>

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

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

3.spring-cxf.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                    http://www.springframework.org/schema/context   
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://cxf.apache.org/jaxws  
                    http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- implementor属性里写的是bean的id,以#,后面是BeanName-->
	<jaxws:endpoint implementor="#VerifyServiceImpl" address="/VerifyService" />
</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

package com.hbl.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;

import com.hlb.utils.util.xml.DomUtil;

/**
 * Servlet implementation class WebServiceServlet
 */
public class WebService extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public WebService() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		InputStream in = request.getInputStream();
		try {
			Document dom = DomUtil.getDocumentByInputStream(in);
			String UserName = DomUtil.getNodeByReg(dom.getDocumentElement(), "Body>userVerify>UserInfoData>UserName").getTextContent();
			System.out.println("UserName:"+UserName);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		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());
	}

}

2.启动测试

 

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

2.Dom解析业务数据

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

这个就是本质代码

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

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值