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);
}
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);
}
Share
Improve this question
edited Mar 21 at 1:47
Gagan
asked Mar 20 at 15:28
GaganGagan
1312 silver badges14 bronze badges
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. – talex Commented Mar 20 at 18:33
1 Answer
Reset to default 1There 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399695a4572323.html
评论列表(0条)