基于webservicesoap协议利⽤xml格式⾃定义报⽂进⾏传输和
解析返回报⽂(实例)
⼀.发送⾃定义soap报⽂(内嵌jsonString)获取返回报⽂⼯具类
apiservice.framework.BusinessException;
l.soap.*;
l.transform.Source;
l.transform.Transformer;
l.transform.TransformerFactory;
l.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
/**
* 发送⾃定义soap报⽂(内嵌jsonString)获取返回报⽂⼯具类
*/
public class SoapUtil {
//对接服务地址
private String addressUrl="XXXXXXXX";
public SoapHepler(String addressUrl) {
if(addressUrl!=null&&!"".im())){
this.addressUrl = addressUrl;
}
}
/**
* 添加所需信息
* @param bodyJsonStr 请求体json
* @param svcname 服务名称
* @param svccode 服务编码
* @param svcver 服务版本
* @param orisys 来源系统标识
* @param trantime 交易发起时间
* @param tranno 全局交易流⽔号
* @param branchno 操作员机构代码op
* @param userid ⽤户IDop
* @param tokenid ⼝令或令牌op
* @param signature 数字签名op
* @param branchnoo 机构代码
* @param destsys ⽬标系统标识
* @return
*/
public String sendSoapXml(String bodyJsonStr,
String svcname, String svccode,
String svcver,String orisys,
String trantime,String tranno,
String branchno,String userid,
String tokenid,String signature,
String branchnoo,String destsys) {
String AUTH_PREFIX = "cl";
String AUTH_NS = "www.e-chinalife/soa/";
String XSD_NS = "/2001/XMLSchema";
String XSD_NS = "/2001/XMLSchema";
String XSI_NS = "/2001/XMLSchema-instance";
try {
//实例化⼀个soap连接对象⼯⼚
SOAPConnectionFactory soapConnFactory = wInstance();
//实例化⼀个连接对象
SOAPConnection connection = ateConnection();
/
/实例化⼀个消息对象
MessageFactory messageFactory = wInstance();
//实例化⼀个消息
SOAPMessage message = ateMessage();
//获取消息中soap消息部分的句柄
SOAPPart soapPart = SOAPPart();
//获取soap消息部分中的信封句柄
SOAPEnvelope envelope = Envelope();
envelope.setPrefix("soapenv");
// envelope.setAttribute("xmlns:xsd", XSD_NS);
envelope.setAttribute("xmlns:xsi", XSI_NS);
envelope.setAttribute("xmlns:soa", AUTH_NS);
//获取信封中需要填写的请求报⽂部分的句柄
//创建soap⼯⼚
SOAPFactory soapFactory = wInstance();
//添加消息体以及json字符串
SOAPBody body = Body();
body.setPrefix("soapenv");
SOAPBodyElement bodyElement = body.ateName("execute", "soa", null)); SOAPElement requestData = bodyElement.addChildElement("requestData","soa");
requestData.addTextNode(judgeNull(bodyJsonStr));
//添加信息头内容
SOAPHeader header = Header();
header.setPrefix("soapenv");
SOAPElement wsHeaderElm = ateElement("HEADER", AUTH_PREFIX, AUTH_NS);
SOAPElement svcnameElm = ateElement("SVCNAME");
SOAPElement svccodeNameElm = ateElement("SVCCODE");
SOAPElement svcverElm = ateElement("SVCVER");
SOAPElement orisysElm = ateElement("ORISYS");
SOAPElement trantimeElm = ateElement("TRANTIME");
SOAPElement trannoElm = ateElement("TRANNO");
SOAPElement authElm = ateElement("AUTH");
SOAPElement securityElm = ateElement("SECURITY");
SOAPElement routeElm = ateElement("ROUTE");
SOAPElement branchnoElm = ateElement("BRANCHNO");
SOAPElement useridElm = ateElement("USERID");
SOAPElement tokenidElm = ateElement("TOKENID");
SOAPElement signatureElm = ateElement("SIGNATURE");
SOAPElement branchnooElm = ateElement("BRANCHNO");
SOAPElement destsysElm = ateElement("DESTSYS");
//为上述消息头标签添加节点⽂本值
svcnameElm.addTextNode(judgeNull(svcname));
svccodeNameElm.addTextNode(judgeNull(svccode));
svcverElm.addTextNode(judgeNull(svcver));
orisysElm.addTextNode(judgeNull(orisys));
trantimeElm.addTextNode(judgeNull(trantime));
trannoElm.addTextNode(judgeNull(tranno));
branchnoElm.addTextNode(judgeNull(branchno));
branchnoElm.addTextNode(judgeNull(branchno));
useridElm.addTextNode(judgeNull(userid));
tokenidElm.addTextNode(judgeNull(tokenid));
signatureElm.addTextNode(judgeNull(signature));
branchnooElm.addTextNode(judgeNull(branchnoo));
destsysElm.addTextNode(judgeNull(destsys));
authElm.addChildElement(branchnoElm);
authElm.addChildElement(useridElm);
authElm.addChildElement(tokenidElm);
securityElm.addChildElement(signatureElm);
routeElm.addChildElement(branchnooElm);
routeElm.addChildElement(destsysElm);
wsHeaderElm.addChildElement(svcnameElm);
wsHeaderElm.addChildElement(svccodeNameElm);
wsHeaderElm.addChildElement(svcverElm);
wsHeaderElm.addChildElement(orisysElm);
wsHeaderElm.addChildElement(trantimeElm);
wsHeaderElm.addChildElement(trannoElm);
wsHeaderElm.addChildElement(authElm);
wsHeaderElm.addChildElement(securityElm);
wsHeaderElm.addChildElement(routeElm);
header.addChildElement(wsHeaderElm);
message.saveChanges();
System.out.println("输出报⽂,如下:");
message.writeTo(System.out);
//发送信息
SOAPMessage call = connection.call(message, addressUrl);
TransformerFactory wInstance(); Transformer transformer = wTransformer();
Source sourceContent = SOAPPart().getContent();
StreamResult result = new StreamResult(new ByteArrayOutputStream());
//获取返回报⽂
String s = ((ByteArrayOutputStream) OutputStream()).toString();
System.out.println("返回报⽂,如下:");
System.out.print( s);
connection.close();
return s;
} catch (Exception e) {
throw new BusinessException("返回报⽂失败 ,错误信息:"+e);
}
}
/**添加上述节点⽂本是不能为null
* 如果str为null 置为“”
* @param str
* @return
*/
public static String judgeNull(String str){
return (str==null)?"":str;
}
}
⼆. 解析返回soap报⽂,获取⽬标节点值
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
l.sax.InputSource;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/
**
* 解析返回soap报⽂,获取⽬标节点值
*/
public class XMLUtil {
/**
* 将字符串类型的XML 转化成Docunent⽂档结构</p>
* @param parseStrXml 待转换的xml 字符串
* @return
*/
public static Document strXmlToDocument(String parseStrXml){
StringReader read = new StringReader(parseStrXml);
/
/创建新的输⼊源SAX 解析器将使⽤ InputSource 对象来确定如何读取 XML 输⼊
InputSource source = new InputSource(read);
//创建⼀个新的SAXBuilder
SAXBuilder sb = new SAXBuilder(); // 新建⽴构造器
Document doc = null;
try {
doc = sb.build(source);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return doc;
}
/**
* 根据⽬标节点名获取值
* @param doc ⽂档结构
* @param finalNodeName 最终节点名
* @return
*/
public static String getValueByElementName(Document doc,String finalNodeName){ Element root = RootElement();
HashMap<String,Object> map=new HashMap<String,Object>();
//调⽤getChildAllText⽅法。获取⽬标⼦节点的值
Map<String,Object> resultmap=getChildAllText(doc, root,map);
String result=((finalNodeName);
return result;
}
/**
* 递归获得⼦节点的值
* @param doc ⽂档结构
* @param e 节点元素
* @param resultmap 递归将值压⼊map中
* @param resultmap 递归将值压⼊map中
* @return
*/
public static Map<String ,Object> getChildAllText(Document doc, Element e,HashMap<String,Object> resultmap) {
if (e != null)
{
if (e.getChildren() != null) //如果存在⼦节点
{
List<Element> list = e.getChildren();
for (Element el : list) //循环输出
{
Children().size() > 0) //如果⼦节点还存在⼦节点,则递归获取
{
getChildAllText(doc, el,resultmap);
}
else
{
resultmap.Name(), el.getTextTrim()); //将叶⼦节点值压⼊map
}
}
}
}
return resultmap;调用webservice服务
}
}
三.测试controller
insurance.chinalifeinsurance.utils.SoapHepler;
insurance.chinalifeinsurance.utils.XMLUtil;
import org.apache.axis.AxisFault;
import org.jdom.Document;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
SimpleDateFormat;
import java.util.Date;
/
**
* just for test by fangyi
*/
@RestController
public class ControllerForTest {
@RequestMapping("/testSoap1")
public String getSoapXml() throws AxisFault {
String hospitalRegistrationUrl="xx:xxxx/SOA/CIS/ApplyMDExchange";
Date date = new Date();
SimpleDateFormat ad= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
String format = ad.format(date);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论