最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论