Reputation: 2154
I am trying to save Spring-ws webservice template request to DB so that I can resubmit the same Webservice request when corresponding site is up.
My configs are as below
<bean id="serviceMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>com.XXX.XXX.ws.XXX.submitorder</value>
</list>
</property>
</bean>
I have a XXXIntegrationClientImpl.java
private static final ObjectFactory XXX_INTEGRATION_LOOKUP_FACTORY = new ObjectFactory();
com.XXX.XXX.ws.XXX.submitorder.PlaceExternalSystemOrder request = XXX_INTEGRATION_LOOKUP_FACTORY.createPlaceExternalSystemOrder();
// populate the request with all required values
The partial Source of PlaceExternalSystemOrder
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"orderInfo"})
@XmlRootElement(name = "PlaceExternalSystemOrder")
public class PlaceExternalSystemOrder {...}
I cant modify above since it doesnt belong to us.
Below code doesnt work
JAXBContext context = JAXBContext.newInstance(com.XXX.XXX.ws.XXX.ticketinfo.PlaceExternalSystemOrderResult.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter stringWriter = new StringWriter();
m.marshal(request, stringWriter);
Just wondering what's the equivalent of above to get request XML, so that I can re-submit the request
Upvotes: 1
Views: 2341
Reputation: 16
Create your own "PlaceExternalSystemOrder" class with your own customized fields (xml elements).
Let say PlaceExternalSystemOrder2, use the same annotations @XmlRootElement(name = "PlaceExternalSystemOrder")
from the old class as needed and customize the class to meet the required Request parameters.
The steps would be like:
Create new classes that maps the field to each XmlElements of the request
Customize the new class "PlaceExternalSystemOrder2" i.e add new fields, getters and setters, annotate the field or setters.
Use the Class in createPlaceExternalSystemOrder
Change the JAXBContext context = JAXBContext.newInstance(com.XXX.XXX.ws.XXX.ticketinfo.PlaceExternalSystemOrderResult.class)
to use JAXBContext context = JAXBContext.newInstance(com.XXX.XXX.ws.XXX.ticketinfo.PlaceExternalSystemOrderResult2.class)
Populate the Request object and pass it as request in your m.marshal(request, stringWriter);
- NO Changes here.
Upvotes: 0
Reputation: 2154
Turned out to be a simple solution, unfortunately couldn't find it earlier.
Key was to use Inject configured Jaxb2Marshaller!
@Autowired
private Jaxb2Marshaller serviceMarshaller;
javax.xml.transform.stream.StreamResult result = new StreamResult(new StringWriter());
serviceMarshaller.marshal(request, result);
String xml = result.getWriter().toString();
Upvotes: 2