Java 使用Spring集成创建自定义标头
需要访问请求结构如下所示的SOAP服务 在spring Integration中,我们可以形成身体部分,点击服务并获得响应Java 使用Spring集成创建自定义标头,java,soap,spring-integration,Java,Soap,Spring Integration,需要访问请求结构如下所示的SOAP服务 在spring Integration中,我们可以形成身体部分,点击服务并获得响应 <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <soap:Header>
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"soap:mustUnderstand="1">234
</m:Trans>
<authheader>
<username> uname</username>
<password>password</password>
</authheader>
</soap:Header>
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPriceResponse>
<m:Price>34.5</m:Price>
</m:GetStockPriceResponse>
</soap:Body>
但是如何将头部和身体一起形成,并将其发送到出站网关
有人能帮忙吗
从版本5.0开始,DefaultSoapHeaderMapper支持用户定义的javax.xml.transform.Source类型的头文件,并将其填充为以下文件的子节点:
Map<String, Object> headers = new HashMap<>();
String authXml =
"<auth xmlns='http://test.auth.org'>"
+ "<username>user</username>"
+ "<password>pass</password>"
+ "</auth>";
headers.put("auth", new StringSource(authXml));
...
DefaultSoapHeaderMapper mapper = new DefaultSoapHeaderMapper();
mapper.setRequestHeaderNames("auth");
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<auth xmlns="http://test.auth.org">
<username>user</username>
<password>pass</password>
</auth>
</soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>
protected void populateUserDefinedHeader(String headerName, Object headerValue, SoapMessage target) {
SoapHeader soapHeader = target.getSoapHeader();
if (headerValue instanceof String) {
QName qname = QNameUtils.parseQNameString(headerName);
soapHeader.addAttribute(qname, (String) headerValue);
}
else if (headerValue instanceof Source) {
Result result = soapHeader.getResult();
try {
this.transformerHelper.transform((Source) headerValue, result);
}
catch (TransformerException e) {
throw new SoapHeaderException(
"Could not transform source [" + headerValue + "] to result [" + result + "]", e);
}
}
}