Spring-Integration-DSL & Soap 服务 Soap header 未被调用
Spring-Integration-DSL & Soap Service Soap header is not getting invoked
我正在尝试使用 spring 集成 DSL 调用 soap 服务,需要添加一些自定义 header。
构建了marshellingoutboundgateway。正在尝试覆盖 DefaultSoapHeaderMapper 但 none 被覆盖的方法被调用。
正在尝试构建类似这样的东西。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<ObjectType34 >
</ObjectType34>
</soapenv:Header>
<soapenv:Body>
<ObjectType12 >
</ObjectType12>
</soapenv:Body>
</soapenv:Envelope>
样本位于:https://github.com/spring-projects/spring-integration/blob/master/src/reference/asciidoc/ws.adoc
看起来只在 5.0 中可用
已发布详细代码。
任何见解都会有所帮助。
@Configuration
@SpringBootApplication
@IntegrationComponentScan
@EnableIntegration
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
Info Info = ctx.getBean(Info.class);
//Constructing request Payload
ObjectType12 getInfoType = new ObjectFactory().ObjectType12();
JAXBElement<GetInfoType> getInfoTypeJAXBElement = new ObjectFactory().createGetInfo(getInfoType);
JAXBElement<GetInfoResponseType> getInfoResponseType = Info.getInfo(getInfoTypeJAXBElement);
System.out.println(getInfoResponseType.getName());
ctx.close();
}
@MessagingGateway
public interface Info {
@Gateway(requestChannel = "convert.input")
JAXBElement<GetInfoResponseType> getInfo(JAXBElement<GetInfoType> InfoType);
}
@Bean
public IntegrationFlow convert() {
StringResult result = new StringResult();
return flow -> flow
.wireTap(f -> f.<JAXBElement, String>transform(ele -> {
jaxb2Marshaller().marshal(ele, result);
return result.toString();
}).log())
.handle(endpoint());
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("org.abc", "com.abc");
return marshaller;
}
@Bean
public MarshallingWebServiceOutboundGateway endpoint() {
MarshallingWebServiceOutboundGateway gateway = new MarshallingWebServiceOutboundGateway("https://example.com/v1", jaxb2Marshaller(), jaxb2Marshaller());
gateway.setHeaderMapper(new DefaultSoapHeaderMapper() {
@Override
protected void populateUserDefinedHeader(String headerName, Object headerValue, SoapMessage target) {
super.populateUserDefinedHeader("Content-Type", "application/soap+xml", target);
}
@Override
protected void populateStandardHeaders(Map<String, Object> headers, SoapMessage target) {
headers.put(WebServiceHeaders.SOAP_ACTION,
"http://www.example.com/SOAUI/ServiceHeader/V4");
super.populateStandardHeaders(headers, target);
}
@Override
public void fromHeadersToRequest(MessageHeaders headers, SoapMessage target) {
SaajSoapMessage targetMessage = (SaajSoapMessage) target;
SoapHeader header = targetMessage.getEnvelope().getHeader();
//Constructing SOAP Header
JAXBElement<ObjectType34> trackingHdrTypeJAXBElement = ObjectFactory().createHdr(ObjectType34);
jaxb2Marshaller().marshal(trackingHdrTypeJAXBElement, header.getResult());
System.out.println(header.getResult());
}
@Override
public void setRequestHeaderNames(String... requestHeaderNames) {
super.setRequestHeaderNames("*");
}
});
gateway.setMessageFactory(new SaajSoapMessageFactory() {
@Override
public void setSoapVersion(SoapVersion version) {
super.setSoapVersion(SoapVersion.SOAP_11);
}
});
return gateway;
}
}
更新:修改了代码
@Bean
public DefaultSoapHeaderMapper headerMapper11() {
return new DefaultSoapHeaderMapper() {
@Override
public void fromHeadersToRequest(MessageHeaders headers, SoapMessage target) {
SaajSoapMessage targetMessage = (SaajSoapMessage) target;
SoapHeader header = targetMessage.getEnvelope().getHeader();
//Constructing SOAP Header
JAXBElement<ObjectType34> trackingHdrTypeJAXBElement = ObjectFactory().createHdr(ObjectType34);
jaxb2Marshaller().marshal(trackingHdrTypeJAXBElement, header.getResult());
super.fromHeadersToRequest(headers, target);
}
};
}
并使用方法调用设置 header:
gateway.setHeaderMapper(headerMapper11());
现在被覆盖的方法被调用并且也有 header。
代码现在按预期运行。
我正在尝试使用 spring 集成 DSL 调用 soap 服务,需要添加一些自定义 header。
构建了marshellingoutboundgateway。正在尝试覆盖 DefaultSoapHeaderMapper 但 none 被覆盖的方法被调用。
正在尝试构建类似这样的东西。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<ObjectType34 >
</ObjectType34>
</soapenv:Header>
<soapenv:Body>
<ObjectType12 >
</ObjectType12>
</soapenv:Body>
</soapenv:Envelope>
样本位于:https://github.com/spring-projects/spring-integration/blob/master/src/reference/asciidoc/ws.adoc
看起来只在 5.0 中可用
已发布详细代码。
任何见解都会有所帮助。
@Configuration
@SpringBootApplication
@IntegrationComponentScan
@EnableIntegration
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
Info Info = ctx.getBean(Info.class);
//Constructing request Payload
ObjectType12 getInfoType = new ObjectFactory().ObjectType12();
JAXBElement<GetInfoType> getInfoTypeJAXBElement = new ObjectFactory().createGetInfo(getInfoType);
JAXBElement<GetInfoResponseType> getInfoResponseType = Info.getInfo(getInfoTypeJAXBElement);
System.out.println(getInfoResponseType.getName());
ctx.close();
}
@MessagingGateway
public interface Info {
@Gateway(requestChannel = "convert.input")
JAXBElement<GetInfoResponseType> getInfo(JAXBElement<GetInfoType> InfoType);
}
@Bean
public IntegrationFlow convert() {
StringResult result = new StringResult();
return flow -> flow
.wireTap(f -> f.<JAXBElement, String>transform(ele -> {
jaxb2Marshaller().marshal(ele, result);
return result.toString();
}).log())
.handle(endpoint());
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("org.abc", "com.abc");
return marshaller;
}
@Bean
public MarshallingWebServiceOutboundGateway endpoint() {
MarshallingWebServiceOutboundGateway gateway = new MarshallingWebServiceOutboundGateway("https://example.com/v1", jaxb2Marshaller(), jaxb2Marshaller());
gateway.setHeaderMapper(new DefaultSoapHeaderMapper() {
@Override
protected void populateUserDefinedHeader(String headerName, Object headerValue, SoapMessage target) {
super.populateUserDefinedHeader("Content-Type", "application/soap+xml", target);
}
@Override
protected void populateStandardHeaders(Map<String, Object> headers, SoapMessage target) {
headers.put(WebServiceHeaders.SOAP_ACTION,
"http://www.example.com/SOAUI/ServiceHeader/V4");
super.populateStandardHeaders(headers, target);
}
@Override
public void fromHeadersToRequest(MessageHeaders headers, SoapMessage target) {
SaajSoapMessage targetMessage = (SaajSoapMessage) target;
SoapHeader header = targetMessage.getEnvelope().getHeader();
//Constructing SOAP Header
JAXBElement<ObjectType34> trackingHdrTypeJAXBElement = ObjectFactory().createHdr(ObjectType34);
jaxb2Marshaller().marshal(trackingHdrTypeJAXBElement, header.getResult());
System.out.println(header.getResult());
}
@Override
public void setRequestHeaderNames(String... requestHeaderNames) {
super.setRequestHeaderNames("*");
}
});
gateway.setMessageFactory(new SaajSoapMessageFactory() {
@Override
public void setSoapVersion(SoapVersion version) {
super.setSoapVersion(SoapVersion.SOAP_11);
}
});
return gateway;
}
}
更新:修改了代码
@Bean
public DefaultSoapHeaderMapper headerMapper11() {
return new DefaultSoapHeaderMapper() {
@Override
public void fromHeadersToRequest(MessageHeaders headers, SoapMessage target) {
SaajSoapMessage targetMessage = (SaajSoapMessage) target;
SoapHeader header = targetMessage.getEnvelope().getHeader();
//Constructing SOAP Header
JAXBElement<ObjectType34> trackingHdrTypeJAXBElement = ObjectFactory().createHdr(ObjectType34);
jaxb2Marshaller().marshal(trackingHdrTypeJAXBElement, header.getResult());
super.fromHeadersToRequest(headers, target);
}
};
}
并使用方法调用设置 header:
gateway.setHeaderMapper(headerMapper11());
现在被覆盖的方法被调用并且也有 header。
代码现在按预期运行。