how to use some indirection when unmarshalling json to java bean using Jersey using jaxb annotations

大兔子大兔子 提交于 2019-12-01 17:42:34

Your annotated class should be bijective: it should allow to generate the same input from which it was unmarshalled. If you still want to use a non-bijective object graph, you can use @XmlAnyElement the following way:

public class Issue {

    @XmlElement(required = true)
    protected Fields fields;

    public Fields getFields() {
        return fields;
    }
}

In the input you gave, fields is not a list, but a field (JSON uses [] to delimit lists):

public class Fields {

    @XmlElement(required = true)
    protected Summary summary;

    @XmlAnyElement
    private List<Element> fields;

    public List<Element> getFields() {
        return fields;
    }

    public Summary getSummary() {
        return summary;
    }
}

In order to catch Summary, you'll have to define a dedicated class. Remaining fields will be grouped in the fields list of elements.

public class Summary {

    @XmlAttribute
    protected String name;

    public String getName() {
        return name;
    }
}

Below, a unit test using your input shows that everything work:

public class JaxbTest {
    @Test
    public void unmarshal() throws JAXBException, IOException {
        URL xmlUrl = Resources.getResource("json.txt");
        InputStream stream = Resources.newInputStreamSupplier(xmlUrl).getInput();
        Issue issue = parse(stream, Issue.class);

        assertEquals("summary", issue.getFields().getSummary().getName());

        Element element = issue.getFields().getFields().get(0);
        assertEquals("customfield_10080", element.getTagName());
        assertEquals("name", element.getFirstChild().getLocalName());
        assertEquals("Testeur", element.getFirstChild().getFirstChild().getTextContent());
    }

    private <T> T parse(InputStream stream, Class<T> clazz) throws JAXBException {
        JSONUnmarshaller unmarshaller = JsonContextNatural().createJSONUnmarshaller();
        return unmarshaller.unmarshalFromJSON(stream, clazz);
    }

    private JSONJAXBContext JsonContextNatural() throws JAXBException {
        return new JSONJAXBContext(JSONConfiguration.natural().build(), Issue.class);
    }
}

This tests shows that without using dedicated classes, your code will quickly be hard to read.

You will need those maven dependencies to run it:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>r08</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.6</version>
</dependency>
{
    "expand":"html",
        "self":"xxx/jira/rest/api/latest/issue/EPC-2731";,
        "key":"EPC-2731",
        "fields":{
            "summary":{
                "name":"summary",
                "type":"java.lang.String",
                "value":"Fwd: commentaires vides dans FicheSousGroupement" 
            },
            "timetracking":{
                "name":"timetracking",
                "type":"com.atlassian.jira.issue.fields.TimeTrackingSystemField",
                "value":{
                    "timeestimate":0,
                    "timespent":60 
                } 
            },
            "issuetype":{
                "name":"issuetype",
                "type":"com.atlassian.jira.issue.issuetype.IssueType",
                "value":{
                    "self":"xxx/jira/rest/api/latest/issueType/2";,
                    "name":"Nouvelle fonctionnalité",
                    "subtask":false 
                } 
            },
            "customfield_10080":{
                "name":"Testeur",
                "type":"com.atlassian.jira.plugin.system.customfieldtypes:userpicker" 
            },
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!