您好,我得到了这个Json字串

{"NexusResource":{"resourceURI":"http://nexus.ad.hrm.se/nexus/service/local/repositories/snapshots/content/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/","relativePath":"/se/hrmsoftware/hrm/hrm-release/16.1-SNAPSHOT/","text":"16.1-SNAPSHOT","leaf":false,"lastModified":"2018-04-09 12:23:59.0 UTC","sizeOnDisk":-1}}


我想将其转换为名为NexusResource的类的对象,如下所示

public class NexusResource {
@JsonProperty("resourceURI") private String resourceURI;
@JsonProperty("relativePath") private String relativePath;
@JsonProperty("text") private String text;
@JsonProperty("leaf") private Boolean leaf;
@JsonProperty("lastModified") private String lastModified;
@JsonProperty("sizeOnDisk") private Integer sizeOnDisk;
@JsonIgnore private Map<String, Object> additionalProperties = new HashMap<>();
}


我尝试使用ObjectMapper进行转换

 ObjectMapper mapper = new ObjectMapper();
    NexusResource resource = mapper.readValue(version, NexusResource.class);


版本是Json字符串,但是当我记录资源时,即使版本获得了所有数据,我得到的还是null(空)。

最佳答案

您可以将ObjectMapper配置为解包根值,以便反序列化到POJO中。

例如。:

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);


请参见API

您也可以通过修改POJO来解决此问题(请参见Karol的答案)。

不能选择任何一个都将导致抛出com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException,并显示消息:Unrecognized field "NexusResource"

10-07 20:59