java - Convert Map<String, Map<String, Object>> to class using mapstruct - Stack Overflow

I have an API response which looks like this{"apiResponse": {"successRequestList":

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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399695a4572323.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信