如何将xml反序列化为axis2生成的java对象
我使用 Eclipse 生成一个给定第三方 wsdl 的 java 客户端代码存根。客户端工作得很好,我能够访问网络服务并做我需要做的事情。
现在,我想编写一些无需连接到 Web 服务即可运行的单元测试。是否可以使用 axis2 堆栈中的某种机制将 xml 文件反序列化为 java 客户端存根代码中的 java 对象之一?
例如,客户端存根代码中的类之一是“Contact”。假设我有一个 xml 文件,它模仿通常在肥皂请求中找到的 xml。如何将其反序列化为 java Contact 对象?
我以前使用过 XMLBeans,但希望有一种更简单的方法,因为 java 客户端似乎已经在幕后的某个地方进行了反序列化?也许axis2有一个方法可以获取一块xml并返回一个java对象?
更新:
我尝试了这个:
String packageName = Contact.class.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance( packageName );
我得到了这个:
java.lang.AssertionError: javax.xml.bind.JAXBException: "com.sforce.soap._2006._04.metadata" doesnt contain ObjectFactory.class or jaxb.index
然后,我尝试了这个:
Contact c = new Contact();
JAXBContext jc = JAXBContext.newInstance( c.getClass() );
但是后来我得到了异常,联系人类使用的类之一没有一个无参数的默认构造函数
我希望这将是一件快速而简单的事情,但是在我有时间完全理解 axis2 以及它如何使用 jaxb 之前,我将手动解析 xml。
I used Eclipse to generate a java client code stub given a third party wsdl. The client works great, I'm able to access the webservice an do what I need to do.
Now, I'd like to write some unit tests that can run without needing to be connected to the webservice. Is it possible to use some mechanism within axis2 stack to deserialize a xml file into one of the java objects in the java client stub code?
For example, one of the classes in the client stub code is "Contact". Say I have an xml file that mimics the xml that would usually is found in soap request. How can I deserialize that into a java Contact object?
I've used XMLBeans before, but hoping there's an easier way since it seems that the java client is already doing this deserialization under the hood somewhere? Maybe axis2 has a method to do take a chunk of xml and return a java object?
UPDATE:
I tried this:
String packageName = Contact.class.getPackage().getName();
JAXBContext jc = JAXBContext.newInstance( packageName );
I get this:
java.lang.AssertionError: javax.xml.bind.JAXBException: "com.sforce.soap._2006._04.metadata" doesnt contain ObjectFactory.class or jaxb.index
Then, I tried this:
Contact c = new Contact();
JAXBContext jc = JAXBContext.newInstance( c.getClass() );
But then I get exception that one of the classes that the Contact Class uses does not have a no-arq default constructor
I was hoping this would be a quick and easy thing to do, but until I have time to fully grok axis2 and how it uses jaxb, I'm just gonna parse the xml manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在 Axis 中称为“解组”。看一下 org.apache.axis2.jaxws.message.databinding.JAXBUtils.getJAXBUnmarshaller(JAXBContext context)。一旦有了解组器,您就可以将 XML 反序列化回对象。
This is called "unmarshalling" in Axis. Have a look at org.apache.axis2.jaxws.message.databinding.JAXBUtils.getJAXBUnmarshaller(JAXBContext context). Once you have an unmarshaller, you can deserialize the XML back into objects.