I am trying to turn a DTO to string to store in a database. I am calling:
ObjectMapper mapper = new ObjectMapper();
TypeFactory factory = mapper.getTypeFactory();
// type of key of response map
JavaType stringType = factory.constructType(String.class);
// type of value of response map
JavaType listOfDtosType = factory.constructCollectionType(ArrayList.class, SummonerRankedInfoDTOEntry.class);
// create type of map
JavaType responseType = factory.constructMapType(HashMap.class, stringType, listOfDtosType);
try {
assert json != null;
Map<String, List<SummonerRankedInfoDTOEntry>> response = new ObjectMapper().readValue(json, responseType);
summonerRankedInfoDTO.setIntegerSummonerRankedInfoDTOEntryMap(response);
logger.info("Json has been de-serialized" + summonerRankedInfoDTO.getIntegerSummonerRankedInfoDTOEntryMap());
} catch (IOException e) {
e.printStackTrace();
}
return summonerRankedInfoDTO;
on my DTO which is: http://codebeautify.org/javaviewer/cb5e233b.
Notice the fields:
LeagueDTOEntry {
division = 'V ', isFreshBlood = false, isHotStreak = false, isInactive = false, isVeteran = false, leaguePoints = 0, losses = 32, miniSeries = null, playerOrTeamId = 'TEAM - 77 b4b970 - 5e e2 - 11e4 - 9 d98 - c81f66db96d8 ', playerOrTeamName = 'Team Invertebrate ', wins = 32
}
the isFreshBlood, isHotStreak, isInactive and isVeteran fields.
I call the above code and log the string it returns, which is: https://jsonblob.com/56852fd6e4b01190df4650cd
All the fields above have lost the "is" part: freshBlood, hotStreak... etc.
I can post my DTOs but I've been looking for a long time and have no idea why it's changing their names. I don't think I ever named them without the "is" as the "is" is on the values returned from an API call.
Any help is appreciated, not sure how to make this question less confusing...
EDIT: My LeagueEntryDTO is:
private String division;
private boolean isFreshBlood;
private boolean isHotStreak;
private boolean isInactive;
private boolean isVeteran;
private int leaguePoints;
private MiniSeriesDTOEntry miniSeries;
private String playerOrTeamId;
private String playerOrTeamName;
private int wins;
private int losses;
@Override
public String toString() {
return "LeagueDTOEntry{" +
"division='" + division + '\'' +
", isFreshBlood=" + isFreshBlood +
", isHotStreak=" + isHotStreak +
", isInactive=" + isInactive +
", isVeteran=" + isVeteran +
", leaguePoints=" + leaguePoints +
", losses=" + losses +
", miniSeries=" + miniSeries +
", playerOrTeamId='" + playerOrTeamId + '\'' +
", playerOrTeamName='" + playerOrTeamName + '\'' +
", wins=" + wins +
'}';
}
If you really want to keep the is
prefixes rather than respecting the standard JavaBean conventions, annotate your getters with (for example)
@JsonProperty("isInactive")
Also, your JSON logic is way too complex. You should just have to do
Map<String, List<SummonerRankedInfoDTOEntry>> response =
new ObjectMapper().readValue(
json,
new TypeReference<Map<String, List<SummonerRankedInfoDTOEntry>>>() {});