[英]Good practice to implement SOAP clients in JAVA for very long requests?
I'm trying to follow this post .我正在尝试关注这篇文章。
To reproduce a sample xml request I have in hand.要重现我手头的示例 xml 请求。 The problem is that this particular request is pretty long with very deep structure, and I just get tired of adding child elements and keeping track of the structure with my eyes.
问题是这个特殊的请求很长,结构很深,我只是厌倦了添加子元素并用我的眼睛跟踪结构。
Below is some sample code that summarised what I have been doing.下面是一些示例代码,总结了我一直在做的事情。 The request I am reproducing has at least 5 layers and more than 50 elements.
我正在复制的请求至少有 5 层和 50 多个元素。 It's supposed to be an application form which contains candidates personal information.
它应该是一个包含候选人个人信息的申请表。
// SOAP Envelop...
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement aaa = soapBody.addChildElement("aaaName", "", "http://my.uri.aaa");
SOAPElement bbb = aaa.addChildElement("bbbName", "", "http://my.uri.bbb");
SOAPElement ccc = bbb.addChildElement("cccName");
SOAPElement ddd = ccc.addChildElement("dddName");
//... and so on ...
SOAPElement dddChild1 = ddd.addChildElement("dddChild1Name");
dddChild1.addTextNode("I'm dddChild1");
SOAPElement dddChild2 = ddd.addChildElement("dddChild2Name");
SOAPElement dddGrandChild2 = dddChild2.addChildElement("dddGrandChild2Name");
dddGrandChild2.addTextNode("I'm dddGrandChild2");
//... and so on ...
Could anyone give some advice on how to make it a bit easier to construct/read/manage?任何人都可以就如何使其更容易构建/阅读/管理提供一些建议吗?
If your request is that large, I would recommend creating Java objects from the WSDL or XSD and then using JAXB to marshal the Java objects to the XML. If your request is that large, I would recommend creating Java objects from the WSDL or XSD and then using JAXB to marshal the Java objects to the XML.
Mkyong has a simple JAXB example: https://www.mkyong.com/java/jaxb-hello-world-example/ Mkyong 有一个简单的 JAXB 示例: https://www.mkyong.com/java/jaxb-hello-world-example/
Note: You're going to have to instantiate and populate the objects, but I would think that would be easier to manage than the way you're doing it now.注意:您将不得不实例化和填充对象,但我认为这比您现在的方式更容易管理。
If you're using Eclipse, you can generate the objects using the steps found in this answer: Generate web service java class from WSDL in eclipse If you're using Eclipse, you can generate the objects using the steps found in this answer: Generate web service java class from WSDL in eclipse
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.