Issue
Currently I have a Spring Integration XML file that marshalls Java POJOs to XML using JAXB and then sends them as JMS messages. However, instead of marshalling Java POJOs and sending through JMS I want to send the XML string which will be dynamically generated inside the Java file to JMS in the same integration file below. So I suppose it will probably be done through replacing the existing Marshaller and transformer in the below file for the request but I don't know exactly how.
In short, I would like to know how to send and replace the marshalling of Java POJOs with dynamic XML string.
Update
I research and Found that DOMSource will be the perfect for generating dynamic XML. I am able to do the transform in Java file from DOM to string easily but I cannot do it through Spring Integration. I need to Marshall from String to XML instead of Java Object to XML using the spring integration file below.
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = document.createElement("Root");
document.appendChild(root);
Element foo = document.createElement("Foo");
foo.appendChild(document.createTextNode("Bar"));
root.appendChild(foo);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(document), result);
System.out.println("XML IN String format is: \n" + writer.toString());
My requirement is to transform the result of the DOMSource to String through Spring integration in the below XML. The change might have to be done in the bean id : Verify_Flight_Detail_Req_MarshallingTransformer below in XML somewhere probably.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-stream="http://www.springframework.org/schema/integration/stream"
xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Queue names -->
<util:constant id="Verify_Flight_Detail_SEND_QUEUE"
static-field="cipac.integration.mq.flight.VerifyFlightDetailGateway.SEND_QUEUE" />
<util:constant id="Verify_Flight_Detail_RECEIVE_QUEUE"
static-field="cipac.integration.mq.flight.VerifyFlightDetailGateway.RECEIVE_QUEUE" />
**<!-- Request Marshaller and Transformer -->
<bean id="Verify_Flight_Detail_Req_Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound"
value="cipac.integration.mq.flight.messages.verifyflightdtl.VerifyFlightDetailReq" />
<property name="marshallerProperties" ref="MQ_MARSHALLER_PROPERTIES" />
</bean>
<bean id="Verify_Flight_Detail_Req_MarshallingTransformer"
class="cipac.integration.mq.common.marshalling.MarshallingTransformerFactory"
factory-method="buildMarshallingTransformer">
<constructor-arg ref="Verify_Flight_Detail_Req_Marshaller" />
<constructor-arg>
<ref bean="resultToStringTransformer" />
</constructor-arg>
</bean>**
<!-- Reply Marshaller and Transformer -->
<bean id="Verify_Flight_Detail_Rply_Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound"
value="cipac.integration.mq.flight.messages.verifyflightdtl.VerifyFlightDetailRply" />
<property name="marshallerProperties" ref="MQ_MARSHALLER_PROPERTIES" />
</bean>
<bean id="Verify_Flight_Detail_Rply_UnmarshallingTransformer"
class="cipac.integration.mq.common.marshalling.UnmarshallingTransformerFactory"
factory-method="buildUnmarshallingTransformer">
<constructor-arg ref="Verify_Flight_Detail_Rply_Marshaller" />
</bean>
<int:channel id="Verify_Flight_Detail_Req">
<int:interceptors>
<int:ref bean="cipacOutInterceptor" />
</int:interceptors>
</int:channel>
<int:channel id="Verify_Flight_Detail_Req_Raw">
<int:interceptors>
<int:wire-tap channel="request-response-jms-logger" />
</int:interceptors>
</int:channel>
<int:channel id="Verify_Flight_Detail_Rply_Raw">
<int:interceptors>
<int:wire-tap channel="request-response-jms-logger" />
</int:interceptors>
</int:channel>
<int:channel id="Verify_Flight_Detail_Rply">
<int:interceptors>
<int:ref bean="cipacInInterceptor" />
</int:interceptors>
</int:channel>
<!-- inbound -->
<int:gateway service-interface="cipac.integration.mq.flight.VerifyFlightDetailGateway">
<int:method name="requestReply" request-channel="Verify_Flight_Detail_Req"
reply-channel="Verify_Flight_Detail_Rply" />
</int:gateway>
<int:chain id="Verify_Flight_Detail_Req_Chain"
input-channel="Verify_Flight_Detail_Req"
output-channel="Verify_Flight_Detail_Req_Raw" >
<!-- transform -->
<int:transformer ref="Verify_Flight_Detail_Req_MarshallingTransformer" />
</int:chain>
<!-- outbound -->
<int-jms:outbound-gateway
request-channel="Verify_Flight_Detail_Req_Raw"
reply-channel="Verify_Flight_Detail_Rply_Raw"
request-destination-name="#{Verify_Flight_Detail_SEND_QUEUE}"
reply-destination-name="#{Verify_Flight_Detail_RECEIVE_QUEUE}"
idle-reply-listener-timeout="${integration.mq.iddleTimeout}"
connection-factory="connectionFactory"
message-converter="cipacMessageConverter"
header-mapper="cipacHeaderMapper"
destination-resolver="cipacDestinationResolver"
receive-timeout="${integration.mq.receiveTimeout}"
time-to-live="${integration.mq.timeToLive}"
explicit-qos-enabled="true"
delivery-persistent="false"
/>
<!-- transform -->
<int:transformer input-channel="Verify_Flight_Detail_Rply_Raw"
output-channel="Verify_Flight_Detail_Rply" ref="Verify_Flight_Detail_Rply_UnmarshallingTransformer" />
That's how the POJO is currently that is getting marshalled.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class VerifyFlightDetailReq {
private static final long serialVersionUID = 1L;
@XmlElement(name = "Flight_Number")
private String flight_number;
@XmlElement(name = "PNR_Number")
private String pnr_number;
@XmlElement(name = "Cust_Full_Name")
private String customerFullName;
Update2:
Please see my answer below that worked for me.
Solution
After a bit of struggle i figure out myself. I used DomSource to generate the XML string and then passed it to JMS through the Custom Transformer and not through JaxB Transformer.
In the Service / Repository class
VerifyFlightReqID req = VerifyFlightReqID();
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = document.createElement("Root");
document.appendChild(root);
Element foo = document.createElement("Foo");
foo.appendChild(document.createTextNode("Bar"));
root.appendChild(foo);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
req.setXmlPayload(writer.toString());
verifyFlightDetailGateway.requestreply(req);
In Transformer class
public class DynamicXmlTransformer {
@Transformer
public Message<String> buildTransformer(Message message) {
//Your Data Type which you send through gateway or you can use just use string to pass XML from your service interface
VerifyFlightReqID req = (VerifyFlightReqID ) message.getPayload();
return MessageBuilder.withPayload(req.xmlpayload).build();
}
Spring Inegration XML change
<bean id="DynamicXmlTransformer" class="cipac.integration.mq.common.DynamicXmlTransformer" />
<int:chain id="Verify_Flight_Detail_Req_Chain"
input-channel="Verify_Flight_Detail_Req"
output-channel="Verify_Flight_Detail_Req_Raw" >
<!-- transform -->
ref="DynamicXmlTransformer" method="buildTransformer"/>
</int:chain>
Answered By - topgun
Answer Checked By - Clifford M. (JavaFixing Volunteer)