JAXB/Moxy Unmarshalling assigns all field values to Maplt;String,Objectgt; rather than the specific field provided for it(JAXB/Moxy Unmarshalling 将所有字段值分配给 Maplt;String,Objectgt;而不是为其提供的特定字段)
问题描述
简而言之,我想执行 unmarshalling
这里提到过,但除了 Map
我还会有一个 @XmlElement
.所以一个字段用 (Map field) @XmlPath(".")
注释,另一个字段用 (String field) @XmlElement
注释,然后我想执行解组
.
In short, I would like to perform the unmarshalling
as mentioned here but along with Map
I will have one more @XmlElement
. So one field is annotated with (Map field) @XmlPath(".")
and another field with (String field) @XmlElement
and then I would like to perform unmarshalling
.
我的应用程序的主要目标是使用 JAXB/Moxy 和 Jackson
转换 XML->JSON
和 JSON->XML
> 图书馆.我正在尝试 unmarshal
XML 并将其映射到 Java POJO.我的 XML 可以有一些专用元素和一些用户定义的元素,这些元素可以随机出现,所以我想将它们存储在 Map
中.因此,我正在使用 XMLAdapter
.我正在关注 博客文章这样做.我做的不完全一样,但有点不同.
My main goal of the application is to convert XML->JSON
and JSON->XML
using the JAXB/Moxy and Jackson
library. I am trying to unmarshal
the XML and map it to the Java POJO. My XML can have some dedicated elements and some user-defined elements which can appear random so I would like to store them in Map<String, Object>
. Hence, I am making use of XMLAdapter
. I am following the blog article to do so. I am not doing exactly the same but a bit different.
我面临的问题是在 unmarshalling
期间根本没有考虑专用字段.所有值都unmarshalled
到Map
.据我了解,这是由于注释 @XmlPath(".")
和 XMLAdapter
的使用而发生的,但是如果我删除此注释,则它将无法正常工作预期的.有人可以帮我解决这个问题吗?marshaling
与 @XmlPath(".")
和 XMLAdapter
都可以正常工作.该问题仅在 unmarshalling
期间出现.
The problem I am facing is during unmarshalling
the dedicated fields are not taken into consideration at all. All the values are unmarshalled
to Map<String.Object>
. As per my understanding it's happening because of the annotation @XmlPath(".")
and usage of XMLAdapter
but If I remove this annotation then it won't work as expected. Can someone please help me with this issue? The marshaling
works fine with both @XmlPath(".")
and XMLAdapter
. The problem is arising only during unmarshalling
.
以下是我想转换为 JSON
的 XML
:(注意:Name
和 Age
是专用字段,而 others
是 user-defined
字段.)
Following is my XML
that I would like to convert to JSON
: (Note: Name
and Age
are dedicated fields and others
is the user-defined
field.)
以下是我的 Customer
类,用于 marshaling
、unmarshalling
由 Moxy
和 Jackson
:(注意:Name
和Age
是专用字段,others
是user-defined
字段.我希望 others
仅存储无法直接映射到 POJO 的值,例如 google:main
及其来自上述 XML 的子项)
Following is my Customer
class used for marshaling
, unmarshalling
by Moxy
and Jackson
: (Note: Name
and Age
are dedicated fields and others
is the user-defined
field. I want others
to store only the values that cannot be mapped directly to POJO such as google:main
and its children from above XML)
以下是我的 TestAdapter
类,它将用于 Userdefined
字段:
Following is my TestAdapter
class which will be used for the Userdefined
fields:
最后,我的 Main
类将用于 marshaling
和 unmarshalling
.另外,要转换为 JSON 和 XML.
And finally, my Main
class will be used for marshaling
and unmarshalling
. Also, to convert to JSON and XML.
当我转换 XML->JSON
时,我得到以下输出:(如果您观察字段 name
和 age
是不作为 Customer
类的专用字段,而是作为随机字段并写入 others
)
When I convert the XML->JSON
then I get the following output: (If you observe the fields name
and age
are not taken as the dedicated fields from Customer
class rather its taken as random fields and written within the others
)
我希望我的输出是这样的:(我希望先映射我的专用字段,然后如果有任何未知字段,然后稍后在 others MAP
中映射它们).请注意,我不想在我的 JSON
中获取 others
标记.我只想获取专用字段的字段名称.
I want my output to be something like this: (I want my dedicated fields to be mapped first then if there are any unknown fields then map them later within others MAP
). Please note that I do not want to get others
tag within my JSON
. I want to get the names of the fields only for the dedicated fields.
以下是我希望在 marshaling
期间获得的 XML
.另外,请注意我正在使用 @XmlPath(".")
以便在我的 XML
中没有得到 others
节点编组
.
Following is the XML
that I would like to get during the marshaling
. Also, please note I am using @XmlPath(".")
so that I do not get the others
node within my XML
during marshaling
.
marshaling
工作正常.问题是在 .unmarshaling
期间发生的,据我了解,这是因为带有 XMLAdapter
的注释 @XmlPath(".")
但是如果我删除此注释,那么它将无法按预期工作.有人可以帮我解决这个问题吗?
The marshaling
is working fine. The problem is happening during .unmarshaling
As per my understanding it's happening because of the annotation @XmlPath(".")
with XMLAdapter
but If I remove this annotation then it won't work as expected. Can someone please help me with this issue?
** 已编辑 **
我想了一些解决方法,但似乎没有什么对我有用.由于 @XmlPath(".")
,它们变得一团糟.仍在寻找一些想法或解决方法.任何帮助将不胜感激.
I thought of a few workarounds but nothing seems to work for me. They are getting messed up due to @XmlPath(".")
. Still looking for some idea or workarounds. Any help would be really appreciated.
推荐答案
啊,终于松了口气.这个问题让我很头疼,但我终于找到了解决方法.尝试了很多东西并联系了很多人,但似乎没有任何效果,我认为这是 JAXB/Moxy
库的问题.我能够找到解决方法.希望它对将来的人有所帮助,不要像我一样感到沮丧:)
ah, finally some relief. This issue ate my head a lot but I was finally able to find a workaround. Tried a lot of things and reached out to many people but nothing seems to work and I thought it's an issue from the JAXB/Moxy
library. I was able to find a workaround. Hope it helps someone in the future and do not get frustrated like me :)
我使用了 2 个字段,一个带有 @XmlAnyElement(lax=true) List