Reputation: 524
my server outputs the following JSON object: (ContentType = application/json)
{"Name":["Name1","Name2"]}
This is my Java code for trying to read that into a POJO.
It creates an instance of the Names class but the array inside is null.
What am I missing here?
restTemplate = new RestTemplate();
uri = "http://localhost:80/api/names";
Names namesWrapperInstance = restTemplate.getForObject(uri, Names.class);
if(namesWrapperInstance != null && namesWrapperInstance.getNames() != null) {
for(String name : namesWrapperInstance.getNames()) {
System.out.print(name);
}
}
@JsonRootName(value = "Name")
class Names {
private String[] Names;
public Names() {
}
public String[] getNames() {
return Names;
}
public void setNames(String[] Names) {
this.Names = Names;
}
}
Upvotes: 1
Views: 2600
Reputation: 691625
Remove the JsonRootName annotation, respect the Java naming conventions in your Java code, and annotate the names
field with @JsonProperty("Name")
.
I'd strongly suggest to use collections rather than arrays, too.
If you can, you should really refactor the JSON, too. Use lowercase for fields, just as in Java, and rename it to names
, since it's an array, containing several names, and not just one. If you do that, the JsonProperty
annotation won't even be necessary anymore.
Upvotes: 3