我正在使用Spring WS创建一个soap webservice。我需要在我的肥皂体上添加多个子元素。目前我只使用另外一个包装元素。
我应该回复的回复示例:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
<soap:Body>
<Response>
<ResponseResult href="#id1"/>
</Response>
<Result id="id1">
...
</Result>
</soap:Body>
</soap:Envelope>
但我只能这样做:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
<soap:Body>
<Wrapper>
<Response>
<ResponseResult href="#id1"/>
</Response>
<Result id="id1">
...
</Result>
</Wrapper>
</soap:Body>
</soap:Envelope>
有人知道如何解决这个问题吗?您需要其他信息吗?
答案 0 :(得分:0)
我终于解决了它。我基本上和这篇文章中做的一样:https://stackoverflow.com/a/15741795/6931969但我会提供更多信息。
我扩展了EndpointInterceptorAdapter并更改了方法handleResponse(MessageContext messageContext, Object endpoint)
。
import org.springframework.stereotype.Component;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.util.LinkedList;
import java.util.List;
@Component
public class CustomEndpointInterceptorAdapter extends EndpointInterceptorAdapter {
/**
* Removes wrapper element.
*/
@Override
public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
System.out.println("--------------------- Interceptor ---------------------");
SOAPMessage soapMsg = ((SaajSoapMessage)messageContext.getResponse()).getSaajMessage();
SOAPBody soapBody = soapMsg.getSOAPBody();
// Get all child nodes of wrapper
Node wrapper = soapBody.getFirstChild();
List<Node> children = getChildren(wrapper);
soapBody.removeContents();
// Add children to SOAPBody
children.forEach(node -> addNode(soapBody, node));
soapMsg.saveChanges();
return true;
}
private List<Node> getChildren(Node wrapper) {
List<Node> result = new LinkedList<>();
NodeList children = wrapper.getChildNodes();
for(int i = 0; i < children.getLength(); i++) {
result.add(children.item(i));
}
return result;
}
private void addNode(SOAPBody soapBody, Node node) {
try {
soapBody.addChildElement((SOAPElement)node);
} catch (SOAPException e) {
System.out.println("Doesn't work for Node: " + node.getNodeName());
e.printStackTrace();
}
}
}