不用任何框架开发web service

很讨厌webservice框架配置的繁琐
尤其是axis系列
一怒之下直接用servlet开发了
其实也很简单,关键是要获取到请求soap和响应soap,可借助soapUI来生成
soapUI的使用这里不做介绍

生成请求soap和响应soap后关键就是解析soap了
直接用的java提供的API来解析
解析请求Soap
代码如下:
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
public class SyncNotifySPResEncoder {

/**
* 返回soap响应
* @param recordSequenceId
* @param resultCode 0: 成功; 1. 失败

*/
public static String encode(String recordSequenceId, int resultCode){
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\">");
ret.append("<soapenv:Header/>")
.append("<soapenv:Body>")
.append("<soap:orderRelationUpdateNotifyResponse soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">")
.append("<orderRelationUpdateNotifyReturn xsi:type=\"rsp:OrderRelationUpdateNotifyResponse\" xmlns:rsp=\"http://rsp.sync.soap.bossagent.vac.unicom.com\">")
.append("<recordSequenceId xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">")
.append(recordSequenceId)
.append("</recordSequenceId>")
.append("<resultCode xsi:type=\"xsd:int\">")
.append(resultCode)
.append("</resultCode>")
.append("</orderRelationUpdateNotifyReturn>")
.append("</soap:orderRelationUpdateNotifyResponse>")
.append("</soapenv:Body>")
.append("</soapenv:Envelope>");

return ret.toString();
}

}


就是这么简单
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值