java - 如何在 SOAPFault 中设置 faultCode?

标签 java soap soapfault

为什么我可以设置一个faulString,但不能在SOAPFault 中设置一个自定义错误代码?当我抛出异常时,文本“Code X”没有出现在 SoapFaultException 中。有人能告诉我为什么吗?谢谢。

SOAPFault soapFault = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createFault();
soapFault.setFaultString("String Y")
soapFault.setFaultCode("Code X");

throw new SOAPFaultException(soapFault);

最佳答案

可以通过以下示例获取 soap 响应中的故障代码:

String faultString = "String Y";
String faultCodeValue = "Code X";
QName faultCode = new QName("nameSpaceURI", faultCodeValue);
SOAPFault soapFault = null;
try {
    soapFault = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL).createFault(faultString, faultCode);
    throw new javax.xml.ws.soap.SOAPFaultException(soapFault);
} catch (SOAPException e1) {
    //
}

我得到以下 soap 错误:

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope" xmlns="">
        <faultcode xmlns:ns0="nameSpaceURI">ns0:Code X</faultcode>
        <faultstring>String Y</faultstring>
        </S:Fault>
    </S:Body>
</S:Envelope>

关于java - 如何在 SOAPFault 中设置 faultCode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555641/

相关文章:

java - 在 Java 中处理巨大的文本文件

java - 如何使用索引文件来提高大文件中的搜索速度?

java - 如何从java中读取字节数组中的数据

c - GMime 多部分内容

web-services - SOAP 错误或结果对象?

java - 如何在 CXF Out Fault Interceptor 中将 HTTP STATUS 代码设置为 500 以外的值?

java - 如何停止 JAXB 限定属性

android - 解析 SOAP 响应时出现非法属性错误

javascript - jquery - 当服务器返回 500 时,XMLHttpRequest 对象中的响应文本为空

web-services - Spring Integration 入站 webservice wsdl 生成