更新 SOAP 1.2 错误 "code value" 和 "locale"?
Updating SOAP 1.2 fault "code value" and "locale"?
我已经设置了一个自定义的 WebFault 异常,如下所示:
@WebFault(name="WSFault", targetNamespace = "http://www.example.com/")
public class WSException extends Exception {
private static final long serialVersionUID = -6647544772732631047L;
private WSFault fault;
并且在端点级别抛出自定义异常时出现以下错误 XML:throw new WSException("1234","My Service Error");
:
<S:Fault xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/">
<S:Code>
<S:Value>S:Receiver</S:Value>
</S:Code>
<S:Reason>
<S:Text xml:lang="en">My Service Error</S:Text>
</S:Reason>
<S:Detail>
<ns2:WSFault xmlns:ns2="http://www.example.com/">
<faultCode>1234</faultCode>
<faultString>My Service Error</faultString>
</ns2:WSFault>
</S:Detail>
</S:Fault>
我想控制 Text
标签中的 xml:lang
值,以允许指定发送错误消息的语言以及代码值 <S:Value>
。有没有办法用 @WebFault
做到这一点?
我能够使用 JAX-WS API 对象并创建自定义故障消息来解决此问题,而不仅仅是抛出异常。这样您就可以按照自己的方式访问和构建所有标签:
// Create a new SOAP 1.2 message from the message factory and obtain the SOAP body
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPBody soapBody = message.getSOAPPart().getEnvelope().getBody();
// get the fault
SOAPFault fault = soapBody.addFault();
// since this is an error generated from the business application
// where SOAPValue is the standard value code "Sender|Reciever...etc"
QName faultName = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, SOAPValue);
fault.setFaultCode(faultName);
// set the fault reason text
// where languageLocale is the passed language local, the Locale object can be used
fault.addFaultReasonText(errorMessage, languageLocale);
// generate the detail
Detail detail = fault.addDetail();
// add the error code entry
QName customCodeEntryName = new QName("http://www.example.com/", "customCode", "ns1");
DetailEntry customCodeEntry = detail.addDetailEntry(customCodeEntryName);
customCodeEntry.addTextNode("this is custom 123 code");
// throw the exception that shall generate the SOAP fault response XML message
throw new SOAPFaultException(fault);
我已经设置了一个自定义的 WebFault 异常,如下所示:
@WebFault(name="WSFault", targetNamespace = "http://www.example.com/")
public class WSException extends Exception {
private static final long serialVersionUID = -6647544772732631047L;
private WSFault fault;
并且在端点级别抛出自定义异常时出现以下错误 XML:throw new WSException("1234","My Service Error");
:
<S:Fault xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/">
<S:Code>
<S:Value>S:Receiver</S:Value>
</S:Code>
<S:Reason>
<S:Text xml:lang="en">My Service Error</S:Text>
</S:Reason>
<S:Detail>
<ns2:WSFault xmlns:ns2="http://www.example.com/">
<faultCode>1234</faultCode>
<faultString>My Service Error</faultString>
</ns2:WSFault>
</S:Detail>
</S:Fault>
我想控制 Text
标签中的 xml:lang
值,以允许指定发送错误消息的语言以及代码值 <S:Value>
。有没有办法用 @WebFault
做到这一点?
我能够使用 JAX-WS API 对象并创建自定义故障消息来解决此问题,而不仅仅是抛出异常。这样您就可以按照自己的方式访问和构建所有标签:
// Create a new SOAP 1.2 message from the message factory and obtain the SOAP body
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPBody soapBody = message.getSOAPPart().getEnvelope().getBody();
// get the fault
SOAPFault fault = soapBody.addFault();
// since this is an error generated from the business application
// where SOAPValue is the standard value code "Sender|Reciever...etc"
QName faultName = new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, SOAPValue);
fault.setFaultCode(faultName);
// set the fault reason text
// where languageLocale is the passed language local, the Locale object can be used
fault.addFaultReasonText(errorMessage, languageLocale);
// generate the detail
Detail detail = fault.addDetail();
// add the error code entry
QName customCodeEntryName = new QName("http://www.example.com/", "customCode", "ns1");
DetailEntry customCodeEntry = detail.addDetailEntry(customCodeEntryName);
customCodeEntry.addTextNode("this is custom 123 code");
// throw the exception that shall generate the SOAP fault response XML message
throw new SOAPFaultException(fault);