很讨厌webservice框架配置的繁琐
尤其是axis系列
一怒之下直接用servlet开发了
其实也很简单,关键是要获取到请求soap和响应soap,可借助soapUI来生成
soapUI的使用这里不做介绍
生成请求soap和响应soap后关键就是解析soap了
直接用的java提供的API来解析
解析请求Soap
代码如下:
写响应SOAP
就是这么简单
尤其是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();
}
}
就是这么简单