0

To explain my issue, let's say that I'm retrieving the following OData V2 Entity:

{
    "d": {
        "EmployeeID": 1,
        "LastName": "Davolio",
        "FirstName": "Nancy",
        "Orders": {
            "results": [
                {
                    "OrderID": 10258
                }
            ]
        },
        "Territories": {
            "results": [
                {
                    "TerritoryID": "06897"
                }
            ]
        }
    }
}

And I have the corresponding model Class:

@JsonRootName(value = "d")
public class Employee {

    @JsonProperty("EmployeeID")
    private int employeeId;
    
    @JsonProperty("LastName")
    private String lastName;
    
    @JsonProperty("FirstName")
    private String firstName;
    
    @JsonProperty("Orders")
    private List<Order> orders;
    
    @JsonProperty("Territories")
    private List<Territory> territories;

...
}

As expected the exception com.fasterxml.jackson.databind.exc.MismatchedInputException is being triggered because the "Orders" and "Territories" collections are actually within the property "results", as the OData V2 states.

Do you guys have any idea how to ignore the nested "results" property and get the lists straight away? Is a custom deserializer or wrapper class really needed in this case?

Thanks!

1 Answers1

0

There are always multiple ways to tackle this problem.

One is to create a wrapper class and have Employee Directly reference it.

For example:

public class WrapperDto<T> implements Serializable {
    @JsonProperty("results")
    private List<T> elements;
}
public class Employee {
...
    @JsonProperty("Orders")
    private WrapperDto<Order> orders;
    
    @JsonProperty("Territories")
    private WrapperDto<Territory> territories;
...
}

when you want to get the orders, you have to call orders.getResults() to get the List

another solution can be found here where you have a custom wrapper: How to map a nested value to a property using Jackson annotations?

Bojan Petkovic
  • 2,406
  • 15
  • 26