尝试在Jackson中反序列化时获取“无法构造实例”

时间:2019-01-26 17:13:04

标签: java jackson jackson-databind

我正试图从gson迁移到jackson。这令人沮丧,杰克逊在序列化方面做得更好(gson会因各种原因而在杰克逊序列化的类上毫无节制地毫无抱怨),但是,gson擅长反序列化。

无法构造org.javautil.core.csv.SqlCsvExporterJson的实例(尽管存在至少一个Creator):没有用于从字符串值反序列化的字符串参数构造函数/工厂方法。

我将其简化为一个简单的bean,我对其进行序列化,然后尝试反序列化。

bean是使用以下命令生成的 http://www.jsonschema2pojo.org/

我的考试班:

package org.wank.core.csv;

import static org.junit.Assert.assertEquals;

import java.util.LinkedHashMap;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class SqlCsvExporterJsonParserTest {


    @Test
    public void testJacksonTwo() throws JsonProcessingException {
        SqlCsvExporterJson exporter = new SqlCsvExporterJson();
        exporter.setExportDateFormatString("yyyy-MM-dd");
        exporter.setDateTimeFormatString("yyyy-MM-dd'T'HH:mm:ssXXX");
        exporter.setSql("select * from a order by nbr");

        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        String json =  mapper.writerWithDefaultPrettyPrinter().writeValueAsString(exporter);
        System.out.println(json);

        SqlCsvExporterJson parser = mapper.convertValue(json, SqlCsvExporterJson.class);
        assertEquals("yyyy-MM-dd",parser.getExportDateFormatString());

    }
}

bean类:

package org.wank.core.csv;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.ToStringBuilder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "exportDateFormatString",
    "dateTimeFormatString",
    "sql",
    "databaseMetaData",
    "columnMeta"
})
public class SqlCsvExporterJson {

    @JsonProperty("exportDateFormatString")
    private String exportDateFormatString;
    @JsonProperty("dateTimeFormatString")
    private String dateTimeFormatString;
    @JsonProperty("sql")
    private String sql;
    @JsonProperty("databaseMetaData")
    private DatabaseMetaData databaseMetaData;
    @JsonProperty("columnMeta")
    private List<ColumnMetum> columnMeta = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * No args constructor for use in serialization
     * 
     */
    public SqlCsvExporterJson() {
    }

    /**
     * 
     * @param exportDateFormatString
     * @param databaseMetaData
     * @param columnMeta
     * @param sql
     * @param dateTimeFormatString
     */
    public SqlCsvExporterJson(String exportDateFormatString, String dateTimeFormatString, String sql, DatabaseMetaData databaseMetaData, List<ColumnMetum> columnMeta) {
        super();
        this.exportDateFormatString = exportDateFormatString;
        this.dateTimeFormatString = dateTimeFormatString;
        this.sql = sql;
        this.databaseMetaData = databaseMetaData;
        this.columnMeta = columnMeta;
    }

    @JsonProperty("exportDateFormatString")
    public String getExportDateFormatString() {
        return exportDateFormatString;
    }

    @JsonProperty("exportDateFormatString")
    public void setExportDateFormatString(String exportDateFormatString) {
        this.exportDateFormatString = exportDateFormatString;
    }

    @JsonProperty("dateTimeFormatString")
    public String getDateTimeFormatString() {
        return dateTimeFormatString;
    }

    @JsonProperty("dateTimeFormatString")
    public void setDateTimeFormatString(String dateTimeFormatString) {
        this.dateTimeFormatString = dateTimeFormatString;
    }

    @JsonProperty("sql")
    public String getSql() {
        return sql;
    }

    @JsonProperty("sql")
    public void setSql(String sql) {
        this.sql = sql;
    }

    @JsonProperty("databaseMetaData")
    public DatabaseMetaData getDatabaseMetaData() {
        return databaseMetaData;
    }

    @JsonProperty("databaseMetaData")
    public void setDatabaseMetaData(DatabaseMetaData databaseMetaData) {
        this.databaseMetaData = databaseMetaData;
    }

    @JsonProperty("columnMeta")
    public List<ColumnMetum> getColumnMeta() {
        return columnMeta;
    }

    @JsonProperty("columnMeta")
    public void setColumnMeta(List<ColumnMetum> columnMeta) {
        this.columnMeta = columnMeta;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("exportDateFormatString", exportDateFormatString).append("dateTimeFormatString", dateTimeFormatString).append("sql", sql).append("databaseMetaData", databaseMetaData).append("columnMeta", columnMeta).append("additionalProperties", additionalProperties).toString();
    }

}

此gson测试:

package org.wank.core.csv;

import static org.junit.Assert.assertEquals;

import org.javautil.core.gson.SqlCsvExporterJsonGson;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class SqlCsvExporterJsonParserGsonTest {

    @Test
    public void testJacksonTwo() throws JsonProcessingException {
        SqlCsvExporterJsonGson exporter = new SqlCsvExporterJsonGson();
        exporter.setExportDateFormatString("yyyy-MM-dd");
        exporter.setDateTimeFormatString("yyyy-MM-dd'T'HH:mm:ssXXX");
        exporter.setSql("select * from a order by nbr");

        Gson mapper = new GsonBuilder().setPrettyPrinting().create();
        String json = mapper.toJson(exporter);
        SqlCsvExporterJsonGson bean = mapper.fromJson(json, SqlCsvExporterJsonGson.class);
        System.out.println(json);
        assertEquals("yyyy-MM-dd",bean.getExportDateFormatString());

    }
}

在此bean上:

package org.wank.core.gson;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang.builder.ToStringBuilder;

public class SqlCsvExporterJsonGson {

    @SerializedName("exportDateFormatString")
    @Expose
    private String exportDateFormatString;
    @SerializedName("dateTimeFormatString")
    @Expose
    private String dateTimeFormatString;
    @SerializedName("sql")
    @Expose
    private String sql;
    @SerializedName("databaseMetaData")
    @Expose
    private DatabaseMetaData databaseMetaData;
    @SerializedName("columnMeta")
    @Expose
    private List<ColumnMetum> columnMeta = null;

    /**
     * No args constructor for use in serialization
     * 
     */
    public SqlCsvExporterJsonGson() {
    }

    /**
     * 
     * @param exportDateFormatString
     * @param databaseMetaData
     * @param columnMeta
     * @param sql
     * @param dateTimeFormatString
     */
    public SqlCsvExporterJsonGson(String exportDateFormatString, String dateTimeFormatString, String sql, DatabaseMetaData databaseMetaData, List<ColumnMetum> columnMeta) {
        super();
        this.exportDateFormatString = exportDateFormatString;
        this.dateTimeFormatString = dateTimeFormatString;
        this.sql = sql;
        this.databaseMetaData = databaseMetaData;
        this.columnMeta = columnMeta;
    }

    public String getExportDateFormatString() {
        return exportDateFormatString;
    }

    public void setExportDateFormatString(String exportDateFormatString) {
        this.exportDateFormatString = exportDateFormatString;
    }

    public String getDateTimeFormatString() {
        return dateTimeFormatString;
    }

    public void setDateTimeFormatString(String dateTimeFormatString) {
        this.dateTimeFormatString = dateTimeFormatString;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public DatabaseMetaData getDatabaseMetaData() {
        return databaseMetaData;
    }

    public void setDatabaseMetaData(DatabaseMetaData databaseMetaData) {
        this.databaseMetaData = databaseMetaData;
    }

    public List<ColumnMetum> getColumnMeta() {
        return columnMeta;
    }

    public void setColumnMeta(List<ColumnMetum> columnMeta) {
        this.columnMeta = columnMeta;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append("exportDateFormatString", exportDateFormatString).append("dateTimeFormatString", dateTimeFormatString).append("sql", sql).append("databaseMetaData", databaseMetaData).append("columnMeta", columnMeta).toString();
    }

}

工作正常。

1 个答案:

答案 0 :(得分:0)

使用readValue方法代替convertValue

SqlCsvExporterJson parser = mapper.readValue(json, SqlCsvExporterJson.class);

有关更多信息,请阅读:

  1. Intro to the Jackson
  2. readValue documentation
  3. convertValue documentation