重复 属性 '' for [simple type, on Deserialization
Duplicate property '' for [simple type, on Deserialization
我目前在使用带有 xml 模块的 Jackson 2.4.4 时遇到问题。
我要反序列化的 XML 看起来像这样:
<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="Whosebug"/>
</categories>
</product>
我的产品 Class 看起来像这样:
@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 ...
下一步是在从文件读取它的过程中映射它:
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());
}
在这样尝试之后,我遇到了这个异常:
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)
接下来是 - 相同的方法 - 相同的 ObjectMapper - 另一个 pojo + xml 工作正常(甚至是列表的)。需要任何帮助或提示,我真的不知道我应该做什么 - 因为异常 Duplicate property ''
不是很好,因为 ''
真的是空的!
解决方案是我从 @JacksonXmlText
切换到 @JacksonXmlProperty
。我误解了 @JacksonXmlText
应该如何使用。
@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;
允许我毫无问题地解析 xml。
我目前在使用带有 xml 模块的 Jackson 2.4.4 时遇到问题。
我要反序列化的 XML 看起来像这样:
<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="Whosebug"/>
</categories>
</product>
我的产品 Class 看起来像这样:
@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 ...
下一步是在从文件读取它的过程中映射它:
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());
}
在这样尝试之后,我遇到了这个异常:
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)
接下来是 - 相同的方法 - 相同的 ObjectMapper - 另一个 pojo + xml 工作正常(甚至是列表的)。需要任何帮助或提示,我真的不知道我应该做什么 - 因为异常 Duplicate property ''
不是很好,因为 ''
真的是空的!
解决方案是我从 @JacksonXmlText
切换到 @JacksonXmlProperty
。我误解了 @JacksonXmlText
应该如何使用。
@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;
允许我毫无问题地解析 xml。