'Spring get value of Hashmap of Hashmap cause deserialization problem

I have a problem while fetching values of a hashmap. I have a YAML like this :

type: deploymentconfig
component:
  template: template.yaml
  parameters:
    REQUESTS_CPU: 1m
autoscaler:
  replicas:
    min: 1
    max: 1
  cpu:
    targetpercentage: 80

It can be like this too, or similar alternative.. :


type: deploymentconfig
component:
  template: template.yaml
  parameters:
    REQUESTS_CPU: 1m

I convert this YAML into my DTO :

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class ResourcesConfigDTO implements Serializable {
  
  private static final long serialVersionUID = 2L;
  
  private String environment;
  private String type;
  private HashMap<String, HashMap<String, Object>> component; // Problem here
  private HashMap<String, HashMap<String, String>> autoscaler;

}

I use this mapper :

private ObjectMapper getYamlMapper() {
        ObjectMapper oMapper = new ObjectMapper(new YAMLFactory());
        oMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        return oMapper;
    }

But i get this error :

Une erreur est survenue sur la méthode : fileContentToResourcesConfigDTO(). Raison : Cannot construct instance of `java.util.HashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('springboot-configmap-secret-component-template.yaml')\n at [Source: (byte[])\"---\ntype: deploymentconfig\ncomponent:\n  template: springboot-configmap-secret-component-template.yaml\n  parameters:\n    REQUESTS_CPU: 1m\n    LIVENESS_PROBE_DELAY: 270\n    GC_MAX_METASPACE_SIZE: 200\n    JAVA_OPTIONS: -XX:MaxRAM=512m -Xmx256m -Djava.net.preferIPv4Stack=true\nautoscaler:\n  replicas:\n    min: 1\n    max: 1\n  cpu:\n    targetpercentage: 80\n\"; line: 4, column: 13] (through reference chain: com.gg.devops.bg.feature.gitlab.application.data.ResourcesConfigDTO[\"component\"]->java.util.HashMap[\"template\"])

My goal is to be able to get values of this DTO. Something like this : String nbReplicas = filteredResults.get(0).getComponent().get("parameters").get("NB_REPLICAS");

Actually if i use private HashMap<String, Object> component;, i don't have deserialization problem. But i can't get values into this hashmap..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source