I have a problem with an XML DTO. I was trying to assemble it in Spring Boot 3.3.5 and jackson-dataformat-xml v2.18.2. The issue here is that I have two lists that contain children with the same name in a DTO.
The DTO looks like this:
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@Builder
public class PlayerActivityDto {
@JacksonXmlProperty(localName = "playerActivityId", isAttribute = true)
private String brandId;
@JacksonXmlProperty(localName = "player")
private PlayerDto player;
@JacksonXmlProperty(localName = "playerActivityDetail")
private PlayerActivityDetailDto playerActivityDetail;
@Setter
@Getter
@JacksonXmlProperty(localName = "summaryBalance")
@JacksonXmlElementWrapper(localName = "startingBalanceList")
private List<SummaryBalanceDto> startingBalanceList;
@Getter
@Setter
@JacksonXmlProperty(localName = "summaryBalance")
@JacksonXmlElementWrapper(localName = "endingBalanceList")
private List<SummaryBalanceDto> endingBalanceList;
@JacksonXmlProperty(localName = "playerMovement")
@JacksonXmlElementWrapper(localName = "playerMovementList")
private List<FundsMovementDto> playerMovementList;
}
The issue i have with is the two lists named endingBalanceList and startingBalanceList. AFAIK, @JacksonXmlProperty(localName = "summaryBalance")
should name the children of lists endingBalanceList and startingBalanceList “summaryBalance” and should produce valid XML.
However, Jackson produces this exception:
Failed to evaluate Jackson serialization for type [class PlayerActivityDto]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Conflicting getter definitions for property “summaryBalance”: PlayerActivityDto#getStartingBalanceList() vs PlayerActivityDto#getEndingBalanceList()
I tried giving these lists their own setters and getters, but it doesn’t seem to help.
EDIT: I have found a workaround
To fix this issue, I had to create a new class to wrap the list in.
PlayerActivityDto
@Builder
public class PlayerActivityDto {
@JacksonXmlProperty(localName = "playerActivityId", isAttribute = true)
private String brandId;
@JacksonXmlProperty(localName = "player")
private PlayerDto player;
@JacksonXmlProperty(localName = "playerActivityDetail")
private PlayerActivityDetailDto playerActivityDetail;
@JacksonXmlProperty(localName = "startingBalanceList")
private SummaryBalanceListWrapper startingBalanceList;
@JacksonXmlProperty(localName = "endingBalanceList")
private SummaryBalanceListWrapper endingBalanceList;
@JacksonXmlProperty(localName = "playerMovement")
@JacksonXmlElementWrapper(localName = "playerMovementList")
private List<FundsMovementDto> playerMovementList;
}
SummaryListWrapper
@Builder
public class SummaryBalanceListWrapper {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "summaryBalance")
private List<SummaryBalanceDto> summaryBalanceList;
}
It seems this issue won’t be addressed by Jackson devs soon, so I hope this helps to other developers running into the same issue.
ludvikjr is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.