-4

I have an API response which looks like this

{
 "apiResponse": {
 "successRequestList": {
 "rides": {
 "bike": null,
 "cars" :{
 "features": {"roof", "Power Steer", "..."},
 "variants": {"petrol", "diesel", "hybrid"},
 "launched" : [
 {
 "city-name": "",
 "country": ""
 }
 ]
 }
 }
 },
 "failedRequestList": {}
 }
}

I want to get the value of cars into the following object using mapstruct.

public class Assets {
 private String validResponse;
 private Drive drive;
}
public class Drive {
 private List<String> features;
 private List<String> variants;
 private List<Availability> availableIn;
}

I'm trying to use mapstruct to map cars to Drive object but not able to find a way to achieve this.

I have tried writing the custom default methods too to achieve this but getting the ClassCastException

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class Assets$Drive (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; Assets$Drive is in unnamed module of loader 'app')

Can someone suggest if this can be achieved easily via Mapstruct or should I prefer any other library. Chat GPT was also not able to provide any viable solution.

The JSON response is saved into the class ResponseDTO

public class ResponseDTO {
 private Map<String, Map<String, Object>> successRequestList;
}

So I need the mapping for this

public interface RideMapper {
 @Mapping(target = "drive", expression = "java((Drive) response.getSuccessRequestList()" +
 ".get(\"rides\").get(\"cars\"))")
 Assets mapToRulesReferenceContext(ResponseDTO response);
}
asked Mar 20, 2025 at 15:28
1
  • 1
    Your response example supposed to be JSON? If so, it is invalid. Anyway you should give us your MapStruct mapping if you want us to fix it. Commented Mar 20, 2025 at 18:33

1 Answer 1

1

There is an error in the JSON. Lists must be enclosed in square brackets [].

Assuming the JSON is correct and you have a Map<...> object, are you sure you need or want to use MapStruct?

MapStruct is typically used for converting between DTO classes (often even identical ones).

Libraries that make HTTP calls and receive a JSON response can usually convert it automatically into a specific class.

If you have to handle the response manually, you can map it to a class without using a Map by leveraging a library like Jackson.

So, let me ask: what is your situation? What method are you using for the HTTP call? Are you the one converting the server response from JSON to Java classes? Are you required to use MapStruct due to some specific requirement?

Keep in mind that Jackson can convert from Map<...> to a specific class as well.

answered Mar 22, 2025 at 13:52
Sign up to request clarification or add additional context in comments.

1 Comment

No, I don't have any such restriction to use the mapstruct, in fact I have used model mapper and it's working fine for me. I was just trying to learn if it's a possibility to use the mapstruct in this situation or not. I guess mapstruct should be used where you have to convert DTO to models and model mapper should be preferred while converting the JSON response to models. Correct?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.