Home > Software design >  MismatchedInputException error Parsing Json
MismatchedInputException error Parsing Json

Time:09-21

In spring boot I get the MismatchedInputException error while trying to parse this json

[
 {
"name": "abcd",
"number": "0.11258868"
 },
 {
"name": "try",
"number": "1.155866887"
 },
 {
"name": "test",
"number": "0.123444"
 }
]

My class is :

@JsonIgnoreProperties
public class Info {
private String name;
private double number;

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}

public Object getNumber() {
    return number;
}

public void setNumber(double number)
{
    this.number= number;
}

The mapping code is simply this:

@GetMapping
public String setInfo()
{
    var info= (List<Info>)m_restTemplate.getForObject(url, Info.class);

    return "test";
}

I know this is very simple code but I couldn't find the solution. What is mismatching with the class?

CodePudding user response:

The issue is with the retrieval part. You are getting a list of Info, but you are saying to Spring RestTemplate that it will get a single Info. Try the following:

@GetMapping
public String setInfo() {
    var info = m_restTemplate.exchange(url, HttpMethod.GET, null, 
        new ParameterizedTypeReference<List<Info>>() {}).getBody();

    return "test";
}
  • Related