My Spring Web Service Client有这个自定义解析器来捕获SoapFaults:
public class MySoapFaultResolver extends SoapFaultMessageResolver implements FaultMessageResolver
{
private static Logger logger = Logger.getLogger( MySoapFaultResolver.class);
@Override
public void resolveFault(WebServiceMessage wsm) throws IOException
{
logger.debug("entering");
// SOAPMessage soapMessage = (SOAPMessage) wsm; cant cast to this
SoapMessage soapMessage = (SoapMessage) wsm;
if( soapMessage == null) {
logger.debug("soapMessage is null");
} else {
logger.debug("soapMessage is not null");
QName om_fc = soapMessage.getFaultCode();
String om_frs = soapMessage.getFaultReason();
logger.debug("om_fc:" + om_fc);
logger.debug("om_frs:" + om_frs);
if( soapMessage.getSoapBody() == null) {
logger.debug("soap body is null");
} else {
logger.debug("soap body is not null");
SoapBody sb = soapMessage.getSoapBody();
logger.debug( sb.toString() ); // prints org.springframework.ws.soap.saaj.SaajSoap11Body@1d23bf4
QName sb_name = sb.getName();
logger.debug("sb_name:" + sb_name);
Iterator<QName> iter_attr_sb = sb.getAllAttributes();
while( iter_attr_sb.hasNext()) {
QName qname = iter_attr_sb.next();
String qname_valu = sb.getAttributeValue(qname);
logger.debug("attribute: " + qname + ":" + qname_valu);
}
if( sb.hasFault()) {
logger.debug("soap body has fault");
SoapFault sff = sb.getFault();
QName fc = sff.getFaultCode();
String fsr = sff.getFaultStringOrReason();
logger.debug("fc:" + fc);
logger.debug("fsr:" + fsr);
Iterator<QName> iter_attr = sff.getAllAttributes();
while( iter_attr.hasNext()) {
QName qname = iter_attr.next();
String qname_valu = sff.getAttributeValue(qname);
logger.debug("attribute: " + qname + ":" + qname_valu);
}
if( sff.getFaultDetail() == null) {
logger.debug("fault has no details");
} else {
logger.debug("fault has details");
SoapFaultDetail faultDetail = sff.getFaultDetail();
Iterator<SoapFaultDetailElement> detailEntries = faultDetail.getDetailEntries();
while( detailEntries.hasNext()) {
SoapFaultDetailElement detailElement = detailEntries.next();
logger.debug("Found SoapFaultDetailElement name:" + detailElement.getName());
}
}
} else {
logger.debug("soap body does not have fault");
}
}
}
logger.debug("exiting");
SoapFaultClientException sfce = new SoapFaultClientException( soapMessage);
throw new IOException( "cursesfoiledagain", sfce);
}
当此错误从服务返回时执行:
<soap:Envelope>
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Could not open connection; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
并将其写入日志文件:
MySoapFaultResolver-resolveFault] - entering
MySoapFaultResolver-resolveFault] - soapMessage is not null
MySoapFaultResolver-resolveFault] - om_fc:null
MySoapFaultResolver-resolveFault] - om_frs:null
MySoapFaultResolver-resolveFault] - soap body is not null
MySoapFaultResolver-resolveFault] - org.springframework.ws.soap.saaj.SaajSoap11Body@1d23bf4
MySoapFaultResolver-resolveFault] - sb_name:{http://schemas.xmlsoap.org/soap/envelope/}Body
MySoapFaultResolver-resolveFault] - soap body does not have fault
MySoapFaultResolver-resolveFault] - exiting
我很困惑为什么代码没有在SoapBody中找到SoapFault。任何人都可以对此有所了解吗? --appended-- 耐人寻味。我还拥有引发此错误的网络服务:
public class MyOutSoapFaultInterceptor extends AbstractSoapInterceptor
{
private static Logger logger = Logger.getLogger( MyOutSoapFaultInterceptor.class);
public MyOutSoapFaultInterceptor()
{
super(Phase.MARSHAL);
}
@Override
public void handleMessage( SoapMessage message) throws Fault
{
logger.debug("entering");
Exception e = message.getContent( Exception.class);
if( e == null) {
logger.debug("e is null");
} else {
logger.debug("e is not null");
logger.debug("e.getCause:" + e.getCause());
logger.debug("e.getMessage:" + e.getMessage());
if( e instanceof Fault) {
logger.debug("e is instanceOf Fault");
Fault f = (Fault) message.getContent( Fault.class);
SoapFault sf = SoapFault.createFault((Fault) e, message.getVersion());
logger.debug("sf is not null");
logger.debug("sf.getCause:" + sf.getCause());
logger.debug("sf.getMessage:" + sf.getMessage());
logger.debug("sf.getStatusCode:" + sf.getStatusCode());
logger.debug("sf.getCode:" + sf.getCode());
FormsEndpointFault newFault = new FormsEndpointFault();
newFault.setCode( sf.getStatusCode());
newFault.setMessage( sf.getMessage());
if( sf.hasDetails()) {
logger.debug("sf has details");
Element eee = sf.getDetail();
if( eee.hasAttributes()) {
NamedNodeMap nnm = eee.getAttributes();
logger.debug("sf.details has " + nnm.getLength() + " attributes");
for( int ii = 0 ; ii < nnm.getLength() ; ii++ ) {
Node nnode = nnm.item(ii); //WARNING Nodes are recursive structures
logger.debug(" attribute node value:" + nnode.getNodeValue());
}
}
if( eee.hasChildNodes()) {
NodeList nl = eee.getChildNodes();
logger.debug("sf.details has " + nl.getLength() + " child nodes");
for( int ii = 0 ; ii < nl.getLength() ; ii++ ) {
Node nnode = nl.item(ii);
logger.debug(" child node value:" + nnode.getNodeValue());
}
}
} else {
logger.debug("sf has no details");
}
}
}
logger.debug("exiting");
}
以及它记录的内容:
handleMessage] - entering
handleMessage] - e is not null
handleMessage] - e.getCause:org.springframework.orm.jpa.JpaSystemException: Could not open connection; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
handleMessage] - e.getMessage:Could not open connection; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
handleMessage] - e is instanceOf Fault
handleMessage] - sf is not null
handleMessage] - sf.getCause:org.springframework.orm.jpa.JpaSystemException: Could not open connection; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
handleMessage] - sf.getMessage:Could not open connection; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
handleMessage] - sf.getStatusCode:500
handleMessage] - sf.getCode:Could not open connection; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
handleMessage] - sf has no details
handleMessage] - exiting
- /所附 -
所以我们在这里看到故障代码在故障离开服务之前设置为500,但没有出现在浏览器中。 TIA,
Still-learning Stev
答案 0 :(得分:4)
我之前处理过类似的问题。最后问题是HTTP状态代码。
如果在处理请求时出现SOAP错误,则为SOAP HTTP 服务器必须发出HTTP 500“内部服务器错误”响应和 在包含SOAP Fault元素的响应中包含SOAP消息 (参见第4.4节)表明SOAP处理错误。
http://www.w3.org/TR/2000/NOTE-SOAP-20000508/
没有正确的HTTP状态代码导致我的客户端库(Metro / Glassfish)完全忽略故障部分。
请注意,这适用于Soap 1.1,但我相信对于Soap 1.2,这又是不同的,如果这与您相关,那么您可以阅读规范,但看起来您正在使用1.1。
答案 1 :(得分:3)
您返回的soap错误缺少定义命名空间,这可能是您的代码无法找到错误的原因。
if( sb.hasFault()) {
logger.debug("soap body has fault");
...
您可能想尝试将其包含在
中<soap:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
答案 2 :(得分:1)
在内部,方法sb.hasFault()
获取SoapBody
的所有子元素,查找名为Fault
的子元素,并使用URI http://schemas.xmlsoap.org/soap/envelope/
定义。不知怎的,它找不到它。
但是,您可以使用以下内容从SoapBody
对象中提取信息:
DOMSource source = (DOMSource)sb.getPayloadSource();
Node fault = source.getNode().getChildNodes().item(0);
String faultcode = fault.getChildNodes().item(0) // "faultcode"
.getChildNodes().item(0) // text node inside "faultcode"
.getNodeValue();
String faultstring = fault.getChildNodes().item(1) // "faultstring"
.getChildNodes().item(0) // text node inside "faultstring"
.getNodeValue();
或者尝试直接投射SoapBody
的孩子:
SoapFault sff = null;
DOMSource source = (DOMSource)sb.getPayloadSource();
Node fault = source.getNode().getChildNodes().item(0);
if(fault instanceof SoapFault) {
sff = (SoapFault)fault;
}
答案 3 :(得分:0)
类似埃斯托的法尔塔和阿尔戈博·佩里亚·塞拉利昂算法
faultDetail.getDetailEntries()。forEachRemaining(
pr -> {
Source ver = pr.getSource();
NodeList lstNode = ((DOMSource) ver).getNode().getChildNodes();
Node data = (Node) lstNode.item(0);