不用任何框架开发web service

很讨厌webservice框架配置的繁琐

尤其是axis系列

一怒之下直接用servlet开发了

其实也很简单,关键是要获取到请求soap和响应soap,可借助soapUI来生成

soapUI的使用这里不做介绍

生成请求soap和响应soap后关键就是解析soap了

直接用的java提供的API来解析

解析请求Soap

代码如下:

Java代码  

  1. public class SyncNotifySPReqDecoder {
  2. private static Logger logger = LoggerFactory.getLogger(SyncNotifySPReqDecoder.class);
  3. public static SyncNotifySPReq decode(InputStream in){
  4. DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
  5. String recordSequenceId = "0";
  6. List<YingZhangReceiptBody> itemList = new ArrayList<YingZhangReceiptBody>();
  7. try {
  8. DocumentBuilder dombuilder=domfac.newDocumentBuilder();
  9. Document doc=dombuilder.parse(in);
  10. Element root=doc.getDocumentElement();
  11. //解析recordSequenceId
  12. NodeList recordSequenceIdNodeList = root.getElementsByTagName("recordSequenceId");
  13. if(recordSequenceIdNodeList.getLength() >= 1){
  14. recordSequenceId = recordSequenceIdNodeList.item(0).getTextContent();
  15. }
  16. //解析item
  17. NodeList itemNodeList = root.getElementsByTagName("item");
  18. for (int i = 0; i < itemNodeList.getLength(); i++) {
  19. Node item = itemNodeList.item(i);
  20. String userIdType = getNodeValue(item, "userIdType");
  21. String userId = getNodeValue(item, "userId");
  22. String sp_productId = getNodeValue(item, "sp_productId");
  23. String updateType = getNodeValue(item, "updateType");
  24. //UserIdType填1 为手机号码 ; UserIdType填2 为伪码
  25. if( "1".equals(userIdType) ){
  26. YingZhangReceiptBody body = new YingZhangReceiptBody(remove86(userId), sp_productId, escapeServiceCode(updateType));
  27. itemList.add(body);
  28. }
  29. }
  30. } catch (Exception e) {
  31. logger.error(e.getMessage(), e);
  32. }
  33. return new SyncNotifySPReq(recordSequenceId, itemList);
  34. }
  35. //获取Node Value
  36. public static String getNodeValue(Node item, String nodeName){
  37. for(Node n=item.getFirstChild(); n != null; n=n.getNextSibling()){
  38. if(n.getNodeType() == Node.ELEMENT_NODE){
  39. if(n.getNodeName().equals(nodeName)){
  40. return n.getTextContent();
  41. }
  42. }
  43. }
  44. return null;
  45. }
  46. }
public class SyncNotifySPReqDecoder {
	private static Logger logger = LoggerFactory.getLogger(SyncNotifySPReqDecoder.class);

	public static SyncNotifySPReq decode(InputStream in){
		DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
		String recordSequenceId = "0";
		List<YingZhangReceiptBody> itemList = new ArrayList<YingZhangReceiptBody>();

		try {
			DocumentBuilder dombuilder=domfac.newDocumentBuilder();
			Document doc=dombuilder.parse(in);

			Element root=doc.getDocumentElement();

			//解析recordSequenceId
			NodeList recordSequenceIdNodeList = root.getElementsByTagName("recordSequenceId");
			if(recordSequenceIdNodeList.getLength() >= 1){
				recordSequenceId = recordSequenceIdNodeList.item(0).getTextContent();
			}

			//解析item
			NodeList itemNodeList = root.getElementsByTagName("item");

			for (int i = 0; i < itemNodeList.getLength(); i++) {
				Node item = itemNodeList.item(i);

				String userIdType = getNodeValue(item, "userIdType");
				String userId = getNodeValue(item, "userId");
				String sp_productId = getNodeValue(item, "sp_productId");
				String updateType = getNodeValue(item, "updateType");

				//UserIdType填1 为手机号码 ; UserIdType填2 为伪码
				if( "1".equals(userIdType) ){
					YingZhangReceiptBody body = new YingZhangReceiptBody(remove86(userId), sp_productId, escapeServiceCode(updateType));
					itemList.add(body);
				}
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return new SyncNotifySPReq(recordSequenceId, itemList);
	}

	//获取Node Value
	public static String getNodeValue(Node item, String nodeName){
		for(Node n=item.getFirstChild(); n != null; n=n.getNextSibling()){
			if(n.getNodeType() == Node.ELEMENT_NODE){
				if(n.getNodeName().equals(nodeName)){
					return n.getTextContent();
				}
			}
		}

		return null;
	}
}

写响应SOAP

Java代码  

  1. public class SyncNotifySPResEncoder {
  2. /**
  3. * 返回soap响应
  4. * @param recordSequenceId
  5. * @param resultCode 0: 成功; 1. 失败
  6. */
  7. public static String encode(String recordSequenceId, int resultCode){
  8. StringBuilder ret = new StringBuilder("<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soap=\"http://soap.bossagent.vac.unicom.com\">");
  9. ret.append("<soapenv:Header/>")
  10. .append("<soapenv:Body>")
  11. .append("<soap:orderRelationUpdateNotifyResponse soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">")
  12. .append("<orderRelationUpdateNotifyReturn xsi:type=\"rsp:OrderRelationUpdateNotifyResponse\" xmlns:rsp=\"http://rsp.sync.soap.bossagent.vac.unicom.com\">")
  13. .append("<recordSequenceId xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">")
  14. .append(recordSequenceId)
  15. .append("</recordSequenceId>")
  16. .append("<resultCode xsi:type=\"xsd:int\">")
  17. .append(resultCode)
  18. .append("</resultCode>")
  19. .append("</orderRelationUpdateNotifyReturn>")
  20. .append("</soap:orderRelationUpdateNotifyResponse>")
  21. .append("</soapenv:Body>")
  22. .append("</soapenv:Envelope>");
  23. return ret.toString();
  24. }
  25. }
时间: 2024-08-28 21:06:51

不用任何框架开发web service的相关文章

JAVA开发Web Service几种框架介绍

在讲Web Service开发服务时,需要介绍一个目前开发Web Service的几个框架,分别为Axis,axis2,Xfire,CXF以及JWS(也就是前面所述的JAX-WS,这是Java6发布所提供的对Web Service服务的一种实现.)前面几项都为开源项目,而其中又以axis2与cxf所最为常用,Axis与XFire已随着技术不断的更替慢慢落幕,而目前也只有axis2和cxf官方有更新,Axis与XFire都已不再更新. 下面就分别介绍下这几个框架之间的区别,以便大家进行更好的选择:

SSM框架开发web项目系列(二) MyBatis真正的力量

前言 上篇SSM框架环境搭建篇,演示了我们进行web开发必不可少的一些配置和准备工作,如果这方面还有疑问的地方,可以先参考上一篇“SSM框架开发web项目系列(一) 环境搭建篇”.本文主要介绍MyBatis的基础内容,包括基本概念.开发步骤.使用实例等.说起MyBatis,工作中做过SSH/SSM相关Web开发的或者正在学习MyBatis的人或多或少都会接触到类似“MyBatis和Hibernate有什么区别?”,“MyBatis和Hibernate哪个更好?”,“为什么Mybatis用的人越来

Web Service学习-CXF开发Web Service实例demo(一)

Web Service是什么? Web Service不是框架.更甚至不是一种技术. 而是一种跨平台,跨语言的规范 Web Service解决什么问题: 为了解决不同平台,不同语言所编写的应用之间怎样调用问题.比如.有一个C语言写的程序.它想去调用java语言写的某个方法. 集中解决:1,远程调用 2.跨平台调用 3,跨语言调用 实际应用: 1.同一个公司的新,旧系统的整合.Linux上的java应用,去调用windows平台的C应用 2,不同公司的业务整合.业务整合就带来不同公司的系统整合.不

开发Web Service的几种方式

本文作者在学习使用Java开发Web Service(不包括Restful)时,由于不知道Java有这么多框架支持开发Web Service一度陷入迷惘,不知道这些框架各有 什么不同,各有什么优缺点.经过几天的查资料.实验.失败.再查资料.再实验的过程,终于有了一个大概的了解,也把自己的学习成果跟大家分享一下: 用Java开发Web Service一般有三种方式,本文在Idea下分别使用三种方式并结合Spring容器实现了三个Demo,下面为大家一一介绍. 1.Axis.XFire和CXF方式

Spring学习(七)——开发Web Service的几种方式

本文作者在学习使用Java开发Web Service(不包括Restful)时,由于不知道Java有这么多框架支持开发Web Service一度陷入迷惘,不知道这些框架各有 什么不同,各有什么优缺点.经过几天的查资料.实验.失败.再查资料.再实验的过程,终于有了一个大概的了解,也把自己的学习成果跟大家分享一下: 用Java开发Web Service一般有三种方式,本文在Idea下分别使用三种方式并结合Spring容器实现了三个Demo,下面为大家一一介绍. 1.Axis.XFire和CXF方式

[Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端(续)

前言 本篇是承接上一篇: [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端 在上一篇粗略地介绍了如何使用Top-Down的方式创建一个web service .  但是对于如何部署及调用,以及一些细节的部分基本上没有介绍. 应某些博友的要求, 也适逢自己有空, 接下来就详细介绍一下整个部分如何进行. 环境准备 JDK 肯定要安装了, 这个就不多讲了. 1. eclipse  3.5.2 对eclipse 版本的要求其实不是很严

使用CXF开发Web Service服务

1.使用CXF开发Web Service服务端 1.1 开发一个Web Service业务接口,该接口要用@WebService修饰 (1)创建一个Java项目MyServer (2)在MyServer项目中创建一个接口HelloWorld package com.xju.ws; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHello(String name); } 1.2 开发

[Axis2与Eclipse整合开发Web Service系列之三] 服务端返回值

前言 在前面的三篇中 [Axis2与Eclipse整合开发Web Service系列之一] 生成Web Service Client(将WSDl 转化成 Java代码) [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端 [Axis2与Eclipse整合开发Web Service系列之二] Top-Down方式,通过WSDL逆向生成服务端(续) 介绍了如何使用 axis2 与 eclipse 的开发web Service .在第

Eclipse+CXF框架开发Web服务实战

一. 说明 采用CXF框架开发webservice. 所用软件及版本如下. ? 操作系统:Window XP SP3. ? JDK:JDK1.6.0_07,http://www.oracle.com/technetwork/java/javase/downloads/index.html. ? Tomcat:apache-tomcat-6.0.14.exe,http://tomcat.apache.org/. ? IDE:eclipse-jee-juno-SR1-win32.zip,http:/