如何使用JAXB / MOXy正确处理将包含“ null”的JSON解组?

时间:2019-02-12 17:24:39

标签: json null jaxb unmarshalling moxy

我正在构建一个简单的JAXB / MOXy应用程序,该应用程序从GitLab服务器获取信息。 我只想用来自GitLab API的JSON响应的信息填充一些字段。

我做了一些观察: 每当响应的根对象中有一个“空”值时, 并且此值未保存到我的POJO字段之一中,整个未编组的结果将为'null'。

在以下情况下有效:
'null'值位于嵌套对象中。
'null'值被保存。

我看着并尝试:

XmlNullPolicy (emptyNodeRepresentsNull = true/false, isSetPerformedForAbsentNode = true/false, xsiNilRepresentsNull = true/false)

但似乎无法解决我对此感兴趣的问题。

POJO:


@XmlRootElement()
@XmlAccessorType(XmlAccessType.FIELD)
public class GitlabProject implements GitlabItem {

    @XmlAttribute(name="id")
    private Long projectId;
    @XmlAttribute(name="name")
    private String projectName;

    @XmlElement(nillable=true)
    @XmlAttribute(name="description")
    private String projectDescription;

    // getters, setters

    public GitlabProject() {}

    public GitlabProject(Long id, String name, String description) {
        this.projectId = id;
        this.projectName = name;
        this.projectDescription = description;
    }

    @Override
    public  String toString() {
        return "GitlabProject {\n" +
                "    projectId: " + projectId + "\n" +
                "    projectName: " + projectName + "\n" +
                "    projectDescription: " + projectDescription + "\n" +
                "}";
    }
}

导致'null'的GitLab响应示例:

{
  "id": 69,
  "description": "A surefire plan to achieve Hello World Peace",
  "name": "Hello World Peace",
  "name_with_namespace": "world / peace",
  "path": "peace",
  "path_with_namespace": "world/peace",
  "created_at": "2019-01-01T12:12:43.836Z",
  "default_branch": "master",
  "tag_list": [],
  "ssh_url_to_repo": "XXXXXXXXXXXXXXXXXXXXXXXXXx",
  "http_url_to_repo": "XXXXXXXXXXXXXXXXXXXx",
  "web_url": "XXXXXXXXXXXXXXXXXXXXX,

  "avatar_url": "asdfasdfasdf",
  "star_count": 0,
  "forks_count": null,
  "last_activity_at": "2019-02-11T12:20:43.836Z",
  "namespace": {
    "id": 6,
    "name": "peace",
    "path": "peace",
    "kind": "group",
    "full_path": "group"
  }
}

“ forks_count”的值为“ null”。
结果:

null

可导致我想要的结果的GitLab响应示例:

{
  "id": 69,
  "description": "A surefire plan to achieve Hello World Peace",
  "name": "Hello World Peace",
  "name_with_namespace": "world / peace",
  "path": "peace",
  "path_with_namespace": "world/peace",
  "created_at": "2019-01-01T12:12:43.836Z",
  "default_branch": "master",
  "tag_list": [],
  "ssh_url_to_repo": "XXXXXXXXXXXXXXXXXXXXXXXXXx",
  "http_url_to_repo": "XXXXXXXXXXXXXXXXXXXx",
  "web_url": "XXXXXXXXXXXXXXXXXXXXX,

  "avatar_url": "asdfasdfasdf",
  "star_count": 0,
  "forks_count": 6,
  "last_activity_at": "2019-02-11T12:20:43.836Z",
  "namespace": {
    "id": 6,
    "name": "peace",
    "path": "peace",
    "kind": "group",
    "full_path": null
  }
}

名称空间嵌套对象中“ full_path”的值为“ null”。
结果:

GitlabProject {
    projectId: 69
    projectName: Hello World Peace
    projectDescription: A surefire plan to achieve Hello World Peace
}

另一个导致我想要的GitLab响应的示例:

{
  "id": 69,
  "description": "A surefire plan to achieve Hello World Peace",
  "name": null,
  "name_with_namespace": "world / peace",
  "path": "peace",
  "path_with_namespace": "world/peace",
  "created_at": "2019-01-01T12:12:43.836Z",
  "default_branch": "master",
  "tag_list": [],
  "ssh_url_to_repo": "XXXXXXXXXXXXXXXXXXXXXXXXXx",
  "http_url_to_repo": "XXXXXXXXXXXXXXXXXXXx",
  "web_url": "XXXXXXXXXXXXXXXXXXXXX,

  "avatar_url": "asdfasdfasdf",
  "star_count": 0,
  "forks_count": 6,
  "last_activity_at": "2019-02-11T12:20:43.836Z",
  "namespace": {
    "id": 6,
    "name": "peace",
    "path": "peace",
    "kind": "group",
    "full_path": null
  }
}

我要保存的字段“名称”的值为“空”。
结果:

GitlabProject {
    projectId: 69
    projectName: null
    projectDescription: A surefire plan to achieve Hello World Peace
}

这是我用来解组响应的代码(在单元测试中)

public class MarshallingUnmarshallingTests {
    private GitlabProject projectU;


    @Before
    public void setup() {
    }

    @After
    public void tearDown() {
        projectU = null;
    }

    @Test
    public void testJsonToObject() throws JAXBException, FileNotFoundException {
        File file = new File("GitlabProjectResponse.json");
        StreamSource json = new StreamSource(file);

        Map<String, Object> properties = new HashMap<String, Object>(2);
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

        JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{GitlabProject.class}, properties);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

        projectU = unmarshaller.unmarshal(json, GitlabProject.class).getValue();
        System.out.println(projectU);
    }
}

我已经阅读了很多文档和示例, 但我确实找到了有关“空”表示的解释 和可注释的注释。

在此先感谢您的回答!

0 个答案:

没有答案