我正在使用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"));
}


}

最佳答案

似乎有点奇怪,因为您提供的样品并没有使肥皂起效。但正如我在示例中看到的那样,有一些参数添加到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模板(通过使用力度完成),并称为“ requestTemplate.vm”

<?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);
}
}
}
}
}


希望对您有所帮助

安杰洛

08-16 22:18