javaxmljsonjacksondeserialization

Duplicate property '' for [simple type, on Deserialization


I'm currently facing an issue using Jackson 2.4.4 with the xml module.

The XML I want to deserialize looks like this:

<product>
    <sku>1000668-1</sku>
    <name/>
    <image>/url/or/something.jpg</image>
    <brand>test</brand>
    <sizeCM>36 mm / 19.5 cm</sizeCM>
    <colors>
        <color id="335061" name="Green"/>
    </colors>
    <genders>
        <gender id="362843" name="M"/>
    </genders>
    <occasions>
        <occasion id="322620" name="First"/>
        <occasion id="322621" name="Second"/>
        <occasion id="322627" name="Thrid"/>
        <occasion id="322628" name="..."/>
        <occasion id="322630" name="next"/>
        <occasion id="369680" name="and again"/>
    </occasions>
    <categories>
        <category id="CGO_010_002" name="Stackoverflow"/>
    </categories>
</product>

And my Product Class looks like this:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "product")
@JsonPropertyOrder({ "sku", "image", "brand", "sizeCM", "colors", "genders",
        "occasions", "categories" })
public class Product {

    @JsonProperty("sku")
    @JacksonXmlText
    private String sku;

    @JsonProperty("image")
    @JacksonXmlText
    private String image;

    @JsonProperty("brand")
    @JacksonXmlText
    private String brand;

    @JsonProperty("sizeCM")
    @JacksonXmlText
    private String sizeCM;

    @JsonProperty("colors")
    @JacksonXmlProperty(localName = "colors")
    private Colors colors;

    @JsonProperty("genders")
    @JacksonXmlProperty(localName = "genders")
    private Genders genders;

    @JsonProperty("occasions")
    @JacksonXmlProperty(localName = "occasions")
    private Occasions occasions;

    @JsonProperty("categories")
    @JacksonXmlProperty(localName = "categories")
    private Categories categories;

    // setter & getter ...

Next step was to map it during reading it from a file:

public static void main(String[] args) {
    JacksonXmlModule module = new JacksonXmlModule();
    XmlMapper xmlMapper = new XmlMapper(module);
         xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    product(xmlMapper);
}

public static void product(XmlMapper xmlMapper){
    String s = "/tmp/product.xml";
    Product test = null;
    try {
        test = xmlMapper.readValue(new File(s), Product.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(test.getSku());
}

After trying it like that I facing this exception:

com.fasterxml.jackson.databind.JsonMappingException: Duplicate property '' for [simple type, class com.example.jackson.model.Product]
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:266)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:241)
at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationConexample.java:394)
at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3169)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3062)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2115)
at com.example.jackson.main.TheMain.product(TheMain.java:32)
at com.example.jackson.main.TheMain.main(TheMain.java:23)

Next thing is - same Method - same ObjectMapper - another pojo + xml works fine (even the list's). Any Help or Hint wanted, I really don't know what I should do - because the exception Duplicate property '' isn't so great because the '' are really empty!


Solution

  • Solution for this was that I switched from @JacksonXmlTextto @JacksonXmlProperty. I misunderstood how @JacksonXmlTextshould be used.

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JacksonXmlRootElement(localName="product")
    @JsonPropertyOrder({ "sku", "image", "brand", "sizeCM", "colors", "genders",
            "occasions", "categories" })
    public class Product {
    
        @JsonProperty("sku")
        @JacksonXmlProperty(localName="sku")
        private String sku;
    
        @JsonProperty("image")
        @JacksonXmlProperty(localName="image")
        private String image;
    
        @JsonProperty("brand")
        @JacksonXmlProperty(localName="brand")
        private String brand;
    
        @JsonProperty("sizeCM")
        @JacksonXmlProperty(localName="sizeCM")
        private String sizeCM;
    
        @JsonProperty("colors")
        @JacksonXmlProperty(localName = "colors")
        @XmlElement(name="colors")
        private Colors colors;
    

    Allowed me to parse the xml without any problem.