将SOAP信封转换为字符串(OpenSAML)

时间:2018-10-10 07:06:52

标签: string soap opensaml envelope

我正在尝试生成SOAP错误,并使用opensaml库将SOAP信封作为字符串返回。  这是我的代码。

XMLObjectBuilderFactory builderFactory = org.opensaml.xml.Configuration.getBuilderFactory();

SOAPObjectBuilder<Envelope> envelopeSOAPObjectBuilder = (SOAPObjectBuilder<Envelope>) builderFactory.getBuilder(
        Envelope.DEFAULT_ELEMENT_NAME);
Envelope envelope = envelopeSOAPObjectBuilder.buildObject();

SOAPObjectBuilder<Fault> faultSOAPObjectBuilder = (SOAPObjectBuilder<Fault>) builderFactory.getBuilder(
        Fault.DEFAULT_ELEMENT_NAME);
Fault fault = faultSOAPObjectBuilder.buildObject();

SOAPObjectBuilder<FaultCode> faultCodeSOAPObjectBuilder = (SOAPObjectBuilder<FaultCode>) builderFactory.getBuilder(
        FaultCode.DEFAULT_ELEMENT_NAME);
FaultCode faultCode = faultCodeSOAPObjectBuilder.buildObject();

SOAPObjectBuilder<FaultString> faultStringSOAPObjectBuilder = (SOAPObjectBuilder<FaultString>) builderFactory.getBuilder(
        FaultString.DEFAULT_ELEMENT_NAME);
FaultString faultString = faultStringSOAPObjectBuilder.buildObject();

SOAPObjectBuilder<Body> bodySOAPObjectBuilder = (SOAPObjectBuilder<Body>) builderFactory.getBuilder(
        Body.DEFAULT_ELEMENT_NAME);
Body body = bodySOAPObjectBuilder.buildObject();

fault.setCode(faultCode);
fault.setMessage(faultString);

body.getUnknownXMLObjects().add(fault);
envelope.setBody(body);

有人可以建议我将肥皂信封<>转换为字符串吗?

1 个答案:

答案 0 :(得分:0)

可以使用导入org.opensaml.xml.util.XMLHelper将肥皂信封转换为字符串;

 System.out.println((XMLHelper.prettyPrintXML(marshallObject(envelope))));

由于我创建故障代码和故障字符串的方式不完整,因此此操作较早无法正常工作。

在fault.setCode(faultCode)之前添加以下行后,代码开始工作。

 faultCode.setValue(FaultCode.CLIENT);
 faultString.setValue(FaultString.DEFAULT_ELEMENT_LOCAL_NAME);