简单说一下soap报文,有两种版本的报文的报头。
SOAP_1_1_PROTOCOL与SOAP_1_2_PROTOCOL
分别对应的是
1.1:
<SOAP-ENV:Envelope xmlns:SOAP ENV="http://schemas.xmlsoap.org/soap/envelope/">
1.2:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body/></env:Envelope>
以下附带一个报文拼装基类。当时写的比较粗略没有写上解析响应报文的通用方法,只有一个接口的解析响应方法。
这个以后有空填坑
/**
* 封装soap报文基类
* 1.1:<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
* 1.2:<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body/></env:Envelope>
* @author huangtao
*创建时间2016年9月2日下午9:07:42
*
*/
public class BaseSOAPMessage {
//内部维护的报文消息类
private SOAPMessage message;
//协议(PROTOCOL1_1,PROTOCOL1_2)
private String protocol;
//方法名
private String option;
//名称空间
private String namespace;
//缩写,前缀
private String prefix;
//报文节点(节点名称,值)
private LinkedHashMap<String,String> elements;
final private static String PROTOCOL1_1=SOAPConstants.SOAP_1_1_PROTOCOL;
final private static String PROTOCOL1_2=SOAPConstants.SOAP_1_2_PROTOCOL;
/**
* 默认创建1_1版本的soap协议报文,option,namespace,prefix参数若都设置了则返回一个含参报文
* @return
* @throws SOAPException
*创建时间:2016年9月2日下午10:04:39
*@author:huangtao
*/
public SOAPMessage buildBaseSOAPMessage() throws SOAPException{
MessageFactory messageFactory = MessageFactory.newInstance();
message = messageFactory.createMessage();
if((!StringUtils.isEmpty(option)&&!StringUtils.isEmpty(namespace)&&!StringUtils.isEmpty(prefix))){
message=addSOAPMessageElement();
}
setMessage(message);
return message;
}
/**
* 指定协议版本生成基础报文,option,namespace,prefix参数若都设置了则返回一个含参报文
* @param protocol
* @param PROTOCOL1_1为1.1的soap协议,PROTOCOL1_2为1.2的soap协议.
* @return
* @throws SOAPException
*创建时间:2016年9月2日下午10:02:50
*@author:huangtao
*/
public SOAPMessage buildBaseSOAPMessage(String protocol ) throws SOAPException{
message = (protocol.equals("PROTOCOL1_1"))?(MessageFactory.newInstance(PROTOCOL1_1).createMessage()):(MessageFactory.newInstance(PROTOCOL1_2).createMessage());
if(((!StringUtils.isEmpty(option)&&!StringUtils.isEmpty(namespace)&&!StringUtils.isEmpty(prefix)))){
message=addSOAPMessageElement();
}
setMessage(message);
return message ;
}
/**
* 将报文转成字节流
* @param message
* @return
*创建时间:2016年9月4日下午12:20:10
*@author:huangtao
*/
public byte[] getSOAPMessageAsBytes( ) {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try {
message.writeTo(byteStream);
} catch (SOAPException ex) {
} catch (IOException ex) {
}
return byteStream.toByteArray();
}
/**
* 将报文转成字符串
* @param message
* @return
*创建时间:2016年9月2日下午9:14:47
*@author:huangtao
*/
public String getSOAPMessageAsString() {
return new String(getSOAPMessageAsBytes( ));
}
/**
* 为soap报文添加节点与值,默认排序以put的先后加入xml节点中
* @param message
* @param elements
* @return
*创建时间:2016年9月2日下午10:49:32
*@author:huangtao
* @throws SOAPException
*/
public SOAPMessage addSOAPMessageElement() throws SOAPException{
//通过SOAPEnvelope有效获取相应的Body和Header等信息
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.setAttribute( "xmlns:"+prefix, namespace);
SOAPBody body = envelope.getBody();
QName eName = new QName(prefix+":"+option);
SOAPBodyElement element = body.addBodyElement(eName);
if(elements!=null){
for(Map.Entry<String, String> entry:elements.entrySet()){
element.addChildElement(entry.getKey()).setValue(entry.getValue());
}
return message;}
return message;
}
/**
*获取接口返回报文中return 的值
* @param soapString
* @return
*/
public static String getResultFromXml(String soapString,String operation) {
MessageFactory msgFactory;
String result = null;
try {
msgFactory = MessageFactory.newInstance();
SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
new ByteArrayInputStream(soapString.getBytes("UTF-8")));
reqMsg.saveChanges();
SOAPBody body = reqMsg.getSOAPBody();
Iterator<SOAPElement> iterator = body.getChildElements();
while (iterator.hasNext()) {
SOAPElement element = iterator.next();
String nodeName = "ns2:"+operation+"Response";
if(nodeName.equals(element.getNodeName())){
Iterator<SOAPElement> iterator1 = element.getChildElements();
while (iterator1.hasNext()) {
SOAPElement elementr = iterator1.next();
if("return".equals(elementr.getNodeName())){
result = elementr.getValue();
}
}
}
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public SOAPMessage getMessage() {
return message;
}
public void setMessage(SOAPMessage message) {
this.message = message;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public LinkedHashMap<String, String> getElements() {
return elements;
}
public void setElements(LinkedHashMap<String, String> elements) {
this.elements = elements;
}
public static void main(String[] args) {
}
}
具体想知道soap报文确切格式可以下载soapui测试接口地址。就可以看到请求和响应的报文内容。
附带官网地址:https://www.soapui.org/