gpt4 book ai didi

web-services - 如何从请求中读取SOAP Header信息并将其添加到Spring Web Services中的响应中

转载 作者:行者123 更新时间:2023-12-04 16:35:56 26 4
gpt4 key购买 nike

我正在从事Spring Web服务。我需要在请求和响应消息中添加一些自定义元素。其外观应如下所示:

<soapenv:Envelope>
<soapenv:Header>
<tid:SplsTID>
<tid:Trantype>123</tid:Trantype>
<tid:Tranver>234</tid:Tranver>
</tid:SplsTID>
</soapenv:Header>
<soapenv:Body>
<get:GetOrderNumberRequest LoggingLevel="REGULAR" MonitorFlag="Y">
<get:Header>
<get:TransactionId>111</get:TransactionId>
<get:SourceSystemId>SOMS</get:SourceSystemId>
<get:DateTime>2011-11-11T11:11:11</get:DateTime>
</get:Header>
<get:Body>
<get:StaplesOrderNumber RangeFlag="N" ReleaseFlag="N">
<get:OrderNumber Count="1" End="11" Start="9"/>
</get:StaplesOrderNumber>
</get:Body>
</get:GetOrderNumberRequest>
</soapenv:Body>
</soapenv:Envelope>

我可以通过修改WSDL文件在请求中的 <tid:SplsTID>下附加 <soapenv:Header>。看起来像这样:
<wsdl:message name="GetOrderNumberRequest">
<wsdl:part element="tns:GetOrderNumberRequest" name="GetOrderNumberRequest">
</wsdl:part>
<wsdl:part element="sch1:SplsTID" name="SplsTID">
</wsdl:part>
</wsdl:message>
<wsdl:message name="GetOrderNumberResponse">
<wsdl:part element="tns:GetOrderNumberResponse" name="GetOrderNumberResponse">
</wsdl:part>
<wsdl:part element="sch1:SplsTID" name="SplsTID">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="ONAS">
<wsdl:operation name="GetOrderNumber">
<wsdl:input message="tns:GetOrderNumberRequest" name="GetOrderNumberRequest">
</wsdl:input>
<wsdl:output message="tns:GetOrderNumberResponse" name="GetOrderNumberResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>

问题是,我想从请求中读取 <tid:SplsTID>部分,并希望将其附加到响应的soap header 部分下,这没有发生。我正在使用基于注释的终点。读取soap header 并将其添加到响应中的代码是什么。

目前我的终点类(class)是:
@Endpoint
public class OrderNumberServiceEndPoint {
public static final String NAMESPACE_URI = "http://schemas.staples.com/onas/getOrderNumber";

/**
* The local name of the expected request.
*/
public static final String REQUEST_LOCAL_NAME = "GetOrderNumberRequest";

/**
* The local name of the created response.
*/
public static final String RESPONSE_LOCAL_NAME = "GetOrderNumberResponse";

private GetOrderNumberService getOrderNumberService;

public void setGetOrderNumberService(
GetOrderNumberService getOrderNumberService) {
this.getOrderNumberService = getOrderNumberService;
}

@PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
public GetOrderNumberResponse processOrderNumberRequest(
GetOrderNumberRequest request) throws Exception {
GetOrderNumberResponse response = null;
try{
response = getOrderNumberService.executeRequest(request);
}catch(CannotCreateTransactionException e){
logger.error(ErrorConstants.ERROR_E17);
throw new ServiceException(ErrorConstants.ERROR_E17);
}
return response;
}

}

让我知道是否需要更多详细信息。任何帮助,将不胜感激。

最佳答案

最终,我成功地从请求中读取了soap header 并将其追加到响应中。这是我的端点方法现在的样子:

 @PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
@ResponsePayload
public GetOrderNumberResponse processOrderNumberRequest(
@RequestPayload GetOrderNumberRequest request,
MessageContext messageContext) throws Exception {
logger.info("Request Received");
// read SOAP Header from request and append in response
SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext
.getRequest();
SoapHeader reqheader = soapRequest.getSoapHeader();
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext
.getResponse();
SoapHeader respheader = soapResponse.getSoapHeader();
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Iterator<SoapHeaderElement> itr = reqheader.examineAllHeaderElements();
while (itr.hasNext()) {
SoapHeaderElement ele = itr.next();
transformer.transform(ele.getSource(), respheader.getResult());
}
// process the request PayLoad
GetOrderNumberResponse response = null;
try {
response = getOrderNumberService.executeRequest(request);
} catch (CannotCreateTransactionException e) {
logger.error(ErrorConstants.ERROR_E17);
throw new ServiceException(ErrorConstants.ERROR_E17);
}
logger.info("Response Sent");
return response;
}

关于web-services - 如何从请求中读取SOAP Header信息并将其添加到Spring Web Services中的响应中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8521104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com