2

Hello i got this Json string

{"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}}

I want to convert this to an object of a class named NexusResource that looks like this

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<>();
}

i try to convert it with an ObjectMapper

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

were version is the Json string but when i log resource all i get is null (null) even though version got all the data.

1
  • may you provide us with you model class? Commented Apr 9, 2018 at 13:14

3 Answers 3

3

You can configure your ObjectMapper to unwrap the root value, in order to de-serialize into your POJO.

E.g.:

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

See API.

You could also work around that by modifying your POJO (see Karol's answer).

Failure to choose either should result in a com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException being thrown, with message: Unrecognized field "NexusResource".

Sign up to request clarification or add additional context in comments.

Comments

1

NexusResource is not a root of your JSON but a key. To make your Java mapping work you should define a wrapping type:

public class NexusResources {
  @JsonProperty("NexusResource") private NexusResource root;
  ...
}

and then use it to map:

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

Comments

1

The problem is that the JSON does not match the class you are trying to parse. Please notice that the JSON has a field called "NexusResource" that has all the other fields. Whereas the class NexusResource.class just has the fields. Two things you can do. Change the JSON to match NexusResource.class, or create a new class that matches the JSON.

1) Change the json to the following.

{"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}

2) Create new class that actually matches your Json.

class NexusResourceJson {

    @JsonProperty("NexusResource ")
    NexusResource resource;

    public NexusResource getResource() {return resource;}
}


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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.