Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Rest Template Json output mapping to Object

When i make the API call using Spring Rest template, getting Json response like below

[
  {
    "Employee Name": "xyz123",       
    "Employee Id": "12345"
  }
]

I was created object to map the json response like below:

public class Test {
    
    @JsonProperty("Employee Name")
    private String employeeName;
    
    @JsonProperty("Employee Id")
    private String employeeId;

}

But I am getting below error when i make rest api call:

JSON parse error: Cannot deserialize instance of com.pojo.Emp out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.pojo.Emp out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1

How to map the Rest template Json response to object when Json has spaces in parameter keys?

like image 890
sateesh kumar Rayapati Avatar asked Mar 10 '26 08:03

sateesh kumar Rayapati


1 Answers

Your JSON response is an array of object since it's wrapped in [], so map the data into a List<Emp>. Here used ParameterizedTypeReference to create the TypeReference of List<Emp>

ResponseEntity<List<Emp>> response = restTemplate.exchange(endpointUrl, 
                                                  HttpMethod.GET,httpEntity,
                                             new ParameterizedTypeReference<List<Emp>>(){}); 
List<Emp> employees = response.getBody();
like image 85
Eklavya Avatar answered Mar 12 '26 23:03

Eklavya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!