Using jaxb to represent a list as a root element

How can we marshal / untie the root element in JSON that contains the list using JAXB?

So JSON will be

{
    "tag" : [
        {
            "id" : "a",
            "id2": "aa" 
        },
        {
            "id" : "b",
            "id2" : "bb" 
        },
        {
            "id" : "c",
            "id2" : "cc" 
        } 
    ] 
}

I am using Apache CXF, which supports JSON through Jettison.

The Java class may look like the one below. You can use the XmlList annotation for the list, and XmlValue for the list in the root element. The problem is that XmlValue does not accept a user-defined type.

@XmlRootElement(name = "tag")
public class test
{
    @XmlList
    @XmlValue
    private List<UserDefinedType> testList;
}

Is there any way around this. I need this to work for un-marshalling incoming JSON. Got this idea from here http://bdoughan.blogspot.com/2010/09/jaxb-collection-properties.html

+3
source share
1 answer

JSON, . , , /unmarshall XML.

@XmlRootElement
public class Test {
    @XmlElement(name = "tag")
    private List<UserDefinedType> testList;
}

public class UserDefinedType {
    @XmlElement(name = "id")
    private String someId;

    @XmlElement(name = "id2")
    private String someId2;
}
+2

Source: https://habr.com/ru/post/1786248/


All Articles