@Builder
@Getter
public class POJOClass {
@NonNull
private List<String> states;
@NonNull
private String country;
@NonNull
private String capital;
public Map<String,String> convertToMap() {
TypeReference<HashMap<String, String>> hashMapType
= new TypeReference<HashMap<String, String>>() {};
return new ObjectMapper().convertValue(this, hashMapType);
}
}
i am trying to convert an instance of the above class into a HashMap<String,String> using instance.convertToMap(), but this fails with error
Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: N/A; line: -1, column: -1] (through reference chain: java.util.HashMap["states"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:3605)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3546)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:3534)
I believe this is because of the List when deserialising to map isnt getting converted to String.
Is there a way where i can convert a POJO to Map<String,String> and i dont want a type of Map<String, Object> ?