RestTemplate如何将XML反序列化为POJO

时间:2017-12-01 00:49:05

标签: java xml-parsing resttemplate

我在春季启动时制作一个实用工具,与Nexus通信以查看一些工件。

调用nexus以查看工件的元数据会产生如下响应:

<org.sonatype.nexus.rest.model.ArtifactInfoResourceResponse>
  <data>
    <presentLocally>true</presentLocally>
    <repositoryId>REPO</repositoryId>
    <repositoryName>REPO</repositoryName>
    <repositoryPath>/path/to/artifact.jar</repositoryPath>
    <mimeType>application/java-archive</mimeType>
    <uploader>svc_uploader</uploader>
    <uploaded>1511914555000</uploaded>
    <lastChanged>1511914555000</lastChanged>
    <size>146847619</size>
    <sha1Hash>137773f47a013d4ab9c6d5c2c25647011ba86929</sha1Hash>
    <md5Hash>da472a41d439772ba898e7b0762b6835</md5Hash>
    <repositories>
      <org.sonatype.nexus.rest.model.RepositoryUrlResource>
        <repositoryId>REPO</repositoryId>
        <repositoryName>REPO</repositoryName>
        <path>/path/to/artifact.jar</path>
        <artifactUrl>https://nexus.internal.org/content/repositories/path/to/artifact.jar</artifactUrl>
        <canView>true</canView>
      </org.sonatype.nexus.rest.model.RepositoryUrlResource>
    </repositories>
    <canDelete>false</canDelete>
  </data>
</org.sonatype.nexus.rest.model.ArtifactInfoResourceResponse>

我一直试图将此映射到我定义的Pojo:

@Data
@NoArgsConstructor
@AllArgsConstructor
@JacksonXmlRootElement(localName = "data")
@JsonIgnoreProperties(ignoreUnknown = true)
public class NexusArtifactData {
    @JacksonXmlProperty(localName = "md5Hash")
    private String md5Hash;
    @JacksonXmlProperty(localName = "sha1Hash")
    private String sha1Hash;
}

通过我一直在做的所有研究,并且修补了我已经完成的各种注释,这应该有效。不幸的是,md5Hashsha1Hash被初始化为null。使用JSON这是微不足道的,如何将此XML反序列化为易于使用的POJO?

0 个答案:

没有答案