简体   繁体   English

将Spring-ws webservicetemplate请求以XML格式保存到数据库

[英]Save Spring-ws webservicetemplate request to DB in XML Format

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. 我试图将Spring-ws Web服务模板请求保存到数据库,以便在相应站点启动时可以重新提交相同的Web服务请求。

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 我有一个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 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 只是想知道上面的内容等同于获取请求XML,以便我可以重新提交请求

Turned out to be a simple solution, unfortunately couldn't find it earlier. 原来是一个简单的解决方案,不幸的是找不到它。

Key was to use Inject configured Jaxb2Marshaller! 关键是要使用Inject配置的Jaxb2Marshaller!

@Autowired
private Jaxb2Marshaller     serviceMarshaller;

javax.xml.transform.stream.StreamResult result = new StreamResult(new StringWriter());
serviceMarshaller.marshal(request, result);
String xml = result.getWriter().toString();

Create your own "PlaceExternalSystemOrder" class with your own customized fields (xml elements). 使用您自己的自定义字段(xml元素)创建自己的“ PlaceExternalSystemOrder”类。

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. 假设PlaceExternalSystemOrder2 ,根据需要使用旧类中的相同批注@XmlRootElement(name = "PlaceExternalSystemOrder") ,并自定义该类以满足所需的Request参数。

The steps would be like: 步骤如下:

  • Create new classes that maps the field to each XmlElements of the request 创建将字段映射到请求的每个XmlElement的新类

  • Customize the new class "PlaceExternalSystemOrder2" ie add new fields, getters and setters, annotate the field or setters. 自定义新类“ PlaceExternalSystemOrder2”,即添加新的字段,getter和setter,注释字段或setter。

  • Use the Class in createPlaceExternalSystemOrder 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) 更改JAXBContext context = JAXBContext.newInstance(com.XXX.XXX.ws.XXX.ticketinfo.PlaceExternalSystemOrderResult.class)以使用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); 填充Request对象,并将其作为请求传递给您的m.marshal(request, stringWriter); - NO Changes here. -这里没有变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM