How to create java object from 'anyType' returned from service using JAXB?

元气小坏坏 提交于 2019-11-28 21:21:23

You can pass that object to Unmarshaller.unmarshal(Node), it should be able to unmarshal it.

You can use @XmlAnyElement(lax=true). This will convert XML with known root elements (@XmlRootElement or @XmlElementDecl) to domain objects. For an example see:

From what I've found with working with XML, anyType could represent any object, so the closest thing you can map it back to is java.lang.Object. (Besides that fact that anyType could technically be a security hole, allowing somebody to inject anything, including a malicious binary into that spot, and nothing will stop it since your schema allows it.)

You are best off to change your schema to allow a mapping to your custom object. This is cleaner both from a programming perspective, a consumption perspective, and a security perspective.

Pending you can't do that, I would recommend storing the type as an attribute of your element. You may need to write custom code then to help you convert the anyType back to that object, but at least you know its type then.

My two cents based off of my experience (mostly in the realm of integration).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!