Spring-ws如何在请求体中为soap动作添加属性

时间:2016-11-10 15:59:27

标签: java spring soap wsdl spring-ws

我使用Spring-WS来使用以下wsdl: https://pz.gov.pl/pz-services/SignatureVerification?wsdl 我已经生成了java类,就像在本教程中一样:https://spring.io/guides/gs/consuming-web-service/

此wsdl文件的文档指定请求必须具有属性callId,并且requestTimestamp设置如下例所示:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tpus="http://verification.zp.epuap.gov.pl"> <soapenv:Header/> <soapenv:Body> <tpus:verifySignature callId="6347177294896046332" requestTimestamp="2014-06-30T12:01:30.048+02:00"> <tpus:doc>PD94bWwgdmVyc2E+</tpus:doc> <tpus:attachments> <tpus:Attachment> <tpus:content>PD94bWwgdmVyc2+</tpus:content> <tpus:name>podpis.xml</tpus:name> </tpus:Attachment> </tpus:attachments> </tpus:verifySignature> </soapenv:Body> </soapenv:Envelope>

我的请求如下:

<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="id-82BA5532C">
            <ns3:verifySignature
                xmlns:ns3="http://verification.zp.epuap.gov.pl"
                xmlns="">
                <doc>PD94bWwgdmVyc2E+</doc>
                <attachments>
                    <Attachment>
                        <content>PD94bWwgdmVyc2+</content>
                        <name>podpis.xml</name>
                    </Attachment>
                </attachments>
            </ns3:verifySignature>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

因此,您可以看到我缺少callId和requestTimestamp属性。如果我发送请求的代码看起来像这样,我怎么能添加它们?

public class TrustedProfileValidator extends WebServiceGatewaySupport {
private static final Logger tpLogger = Logger.getLogger(TrustedProfileValidator.class);

/**
 * Trusted profile validator constructor
 */
public TrustedProfileValidator() {
    tpLogger.info("Trusted profile validator service.");
}

public VerifySignatureResponse validate(byte[] documentInByte64, ArrayOfAttachment arrayOfAttachments) {
    tpLogger.info("Checking trusted profile validation");
    VerifySignature request = new VerifySignature();
    request.setDoc(documentInByte64);
    request.setAttachments(arrayOfAttachments);

    return (VerifySignatureResponse) getWebServiceTemplate().marshalSendAndReceive(
            "https://int.pz.gov.pl/pz-services/SignatureVerification", request,
            new SoapActionCallback("verifySignature"));
}

}

1 个答案:

答案 0 :(得分:1)

这似乎有点奇怪,因为你提供的样本并没有反映肥皂的作用;但正如我在样本中看到的那样,有一些参数被添加到soap体中,并且这些参数未映射到WS模式中

在任何情况下,如果文档说soap操作字符串必须具有这些参数,您仍然可以使用您使用的内容,但必须将属性传递给SoapActionCallback: 例如,您可以执行以下操作

wsTemplate.marshalSendAndReceive("wsUri", youRequestObj, new SoapActionCallback("verifySignature callId=\""+callId+"\"  requestTimestamp=\""+requestTimestamp+"\""));

通过这种方式,spring ws将通过添加2个属性来编写soap动作

但我认为它的肥皂体内容需要修改;所以在这种情况下你可以使用:

  • org.springframework.ws.client.core.WebServiceTemplate
  • WebServiceTemplate的sendSourceAndReceive方法
  • 您的自定义SourceExtractor

例如,您可以使用这样的XML模板(通过使用velocity完成)并调用&#34; requestTemplate.vm&#34;

<?xml version="1.0" encoding="UTF-8" ?>
 <tpus:verifySignature callId="${callId}" requestTimestamp="${timeStamp}" xmlns:tpus="http://verification.zp.epuap.gov.pl">
         <tpus:doc>${doc}</tpus:doc>
             <tpus:attachments>
                 <tpus:Attachment>
                    <tpus:content>${docContent}</tpus:content>
                      <tpus:name>${docName}</tpus:name>
                 </tpus:Attachment>
             </tpus:attachments>
     </tpus:verifySignature>

然后在您的代码中,您可以执行以下操作:

Map<String, Object> params = new HashMap<String, Object>(5);
params.put("callId", "myCallId");
params.put("timeStamp", "thetimeStamp");
params.put("doc", "theDoc");
params.put("docName", "theDocName");
params.put("docContent", "theDocContent");
String xmlRequest = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "requestTemplate.vm", "UTF-8", params).replaceAll("[\n\r]", "");
StreamSource requestMessage = new StreamSource(new StringReader(xmlRequest));
wsTemplate.sendSourceAndReceive("wsUrl", requestMessage,new new SoapActionCallback("verifySignature"),new CustomSourceExtractor());

其中CustomSourceExtractor是您可以阅读SOAP响应的地方

我做了类似的事

public class VieSourceExtractor implements SourceExtractor<YourObj>
{
@Override
public List<YourObj> extractData(Source src) throws IOException, TransformerException
{
XMLStreamReader reader = null;
try
{
reader = StaxUtils.getXMLStreamReader(src);
//read the xml and create your obj
return yourResult;
}
catch (Exception e)
{
throw new TransformerException(e);
}
finally
{
if (reader != null)
{
try
{
reader.close();
}
catch (XMLStreamException e)
{
logger.error("Error " + e.getMessage(), e);
}
}
}
}
}

我希望这可以帮到你

安吉洛