简体   繁体   English

Hsmap无法被CsvMapper识别

[英]Hashmap unrecognized by CsvMapper

I'm trying to serialize an object which contains a HashMap and i get following exception. 我正在尝试序列化包含HashMap的对象,并且出现以下异常。

com.fasterxml.jackson.dataformat.csv.CsvMappingException: Unrecognized column 'tags': known columns: ["_id","insertDate"] (through reference chain.

Also, I don't understand why it's necessary to have a schema when it's about serialization. 而且,我不理解为什么在进行序列化时必须有一个架构。 In my mind, there is no way to have an unrecognized field because all it's specified inside the class. 在我看来,因为在类中指定了所有字段,所以无法拥有无法识别的字段。

Because i don't know well Jackson, i searched and tried various combination of these 2 annotations. 因为我对杰克逊不太了解,所以我搜索并尝试了这2个注释的各种组合。 @JsonUnwrapped @JsonAnyGetter @JsonUnwrapped @JsonAnyGetter

class Product{
    public @Id String _id;

    @JsonUnwrapped
    private HashMap<String, String> tags = new HashMap<>();

    public String insertDate;

    @JsonAnyGetter
    public HashMap<String, String> getTags() {
        return tags;
    }
    @JsonAnySetter
    public void setTags(HashMap<String, String> tags) {
        this.tags = tags;
    }
}

Code used for serialization 用于序列化的代码

CsvSchema schema = csvMapper.schemaFor(Product.class).withHeader();
ObjectWriter myObjectWriter = csvMapper.writer(schema);
csvMapper.enable(CsvParser.Feature.IGNORE_TRAILING_UNMAPPABLE);
csvMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
string = myObjectWriter.writeValueAsString(someProduct);

TRY NR 2 尝试降噪2

public class Product {

public @Id String _id;

public Map<String, String> tags = new HashMap<>();

public String insertDate;

@JsonAnyGetter
public Map<String, String> getTags() {
    return tags;
}
@JsonAnySetter
public void setTags(Map<String, String> tags) {
    this.tags = tags;
}

} }

NEW ERROR 新错误

com.fasterxml.jackson.dataformat.csv.CsvMappingException: CSV generator does not support Object values for properties (nested Objects) (through reference chain: main.model.Product["tags"])

Using Collection implementation class HashMap<String, String> for field type (rather than only for its initial value type ) confuses Jackson. Collection实现类HashMap<String, String>用于字段类型 (而不是仅用于其初始值类型 )会混淆Jackson。

Jackson checks for interface java.util.Map to choose proper Serializer, as evidenced in constructor of 杰克逊检查接口java.util.Map以选择合适的序列化器,如

com.fasterxml.jackson.databind.ser.std.MapSerializer com.fasterxml.jackson.databind.ser.std.MapSerializer

As per Schema question, according to jackson-dataformat-csv docs 根据Schema问题,根据jackson-dataformat-csv文档

It is important to note that the schema object is needed to ensure correct ordering of columns; 重要的是要注意,需要使用架构对象来确保列的正确排序;

Note also that while explicit type can help efficiency it is usually not required, as Jackson data binding can do common conversions/coercions 还要注意,尽管显式类型可以帮助提高效率,但通常不需要这样做,因为Jackson数据绑定可以进行常见的转换/强制转换

I guess ObjectWriter.writeValueAsString(Object) could check reflexively to infer type for serialized object. 我猜想ObjectWriter.writeValueAsString(Object)可以反省检查以推断序列化对象的类型。

Due to tabular nature of CSV format, deeply nested data structures are not well supported. 由于CSV格式的表格性质,因此无法很好地支持深度嵌套的数据结构。

@JsonUnwrapped is for (nested) POJO-s only. @JsonUnwrapped仅用于(嵌套的)POJO-s。 What you want is to use @JsonAnyGetter . 您想要使用@JsonAnyGetter And it needs to return java.util.Map . 并且它需要返回java.util.Map JavaDocs: JavaDocs:

Note that the return type of annotated methods must be Map). 请注意,带注释的方法的返回类型必须为Map。

If you are only serializing, @JsonAnySetter won't be needed. 如果仅序列化,则@JsonAnySetter

I'm curious why are you using Map for tags. 我很好奇您为什么要使用Map作为标签。 Maybe List<String> tags would suffice, tags have only names. 也许List<String> tags就足够了,标签只有名字。

Worth checking out: https://www.baeldung.com/jackson-mapping-dynamic-object 值得一试: https//www.baeldung.com/jackson-mapping-dynamic-object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM