6

I'm making a GET request to a WooCommerce API to fetch orders and my idea is to grab some data from the response and insert it into a local database. I tested the request and everything works correctly. So I continued using QuickType to be able to create the classes corresponding to the response, so I have a way to save it and insert it into the database. The problem is the following, if I comment the "for" statement, there is no problem. But at the moment of using it to obtain the values of the answer and insert them to the database, this error occurs: class java.util.LinkedHashMap cannot be cast to class [name of my class]. And also: [my class] is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader. Can you help me to find a way to solve this problem? Here is the code:

@GetMapping("/ordenes")
    public List<WcOrden> getOrdenes() {

        List<WcOrden> ordenes = (List<WcOrden>) wc.getAll(EndpointBaseType.ORDERS.getValue(), params);
        for (WcOrden orden : ordenes) {
            log.debug("This is an order");
        }
        return ordenes;
    }
2
  • what does wc.getAll method return? If it returns something like java.util.LinkedHashMap then you can't cast it to List<WcOrden> object because LinkedHashMap is not a List. Commented Dec 9, 2021 at 20:12
  • wc.getAll returns a List, but with no generic type inside List getAll(String endpointBase, Map<String, String> params); So I think if its returns a List, I can cast it to List<WcOrden> right? Commented Dec 9, 2021 at 20:14

1 Answer 1

11

I assume wc.getAll(...) is a call to an external API.

When Jackson does not have enough information about what class to deserialize to, it uses LinkedHashMap.

In your case, it knows it is a List, but not the actual type of the elements inside the list, as explained in this article.

So, to solve it, you can use the convertValue of the ObjectMapper:

ObjectMapper mapper = new ObjectMapper(); // or inject it as a dependency
List<WcOrden> pojos = mapper.convertValue(wc.getAll(...), new TypeReference<List<WcOrden>>() { });
Sign up to request clarification or add additional context in comments.

3 Comments

Okay, I understand. I tested it and it seems to work fine, the same error does not occur as before. The problem is that now I have another one. I have been investigating this time the error: Unrecognized field "payment_method_title". From what I understand, in my mapping classes I have to use the @JsonProperties annotation, and I was adding this annotation to all my attributes, corresponding with the json returned by the API. However, I am still getting the same error, that says Unrecognized field "payment_method_title" (class [my class], not marked as ignorable.
If you do not need this field, you can use the following annotation on your WcOrden class: @JsonIgnoreProperties(ignoreUnknown = true)
Well, after adding these annotations in several places where I needed them, it worked correctly. Thank you very much!

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.