postgresReact存储库:无法将列表< object>保存为jsonb

3bygqnnd  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(244)

我的实体包含以下字段:

public class Word {

    @Column("special_characters")
    private List<String> specialCharacters = new ArrayList<>();
``` `special_characters` jsonb在数据库中吗
使用可扩展的React式存储库 `ReactiveCrudRepository<Word, Long>` 为了从/转换为json,我遵循了以下指南,因此要序列化的转换器如下所示:

@WritingConverter
public class ArrayToJsonConverter implements Converter<List, Json> {

private static final Logger LOGGER = LoggerFactory.getLogger(ArrayToJsonConverter.class);
private final ObjectMapper objectMapper;

public ArrayToJsonConverter(ObjectMapper objectMapper) {this.objectMapper = objectMapper;}

@Override
public Json convert(List<Object> source) {
    try {
        return Json.of(objectMapper.writeValueAsBytes(source));
    } catch (JsonProcessingException e) {
        LOGGER.error("Error occurred while serializing array to JSON: {}", source, e);
    }
    return Json.of("");
}

}

以及要反序列化的转换器:

@ReadingConverter
public class JsonToArrayConverter implements Converter<Json, List> {

private static final Logger LOGGER = LoggerFactory.getLogger(MapToJsonConverter.class);
private final ObjectMapper objectMapper;

public JsonToArrayConverter(ObjectMapper objectMapper) {this.objectMapper = objectMapper;}

@Override
public List<Object> convert(Json json) {
    try {
        return objectMapper.readValue(json.asArray(), new TypeReference<>() {});
    } catch (IOException e) {
        LOGGER.error("Problem while parsing JSON to List: {}", json, e);
    }
    return new ArrayList<>();
}

}

这些被添加到 `AbstractR2dbcConfiguration` :

@Override
protected List getCustomConverters() {
List converters = new ArrayList<>();
converters.add(new JsonToArrayConverter(objectMapper));
converters.add(new ArrayToJsonConverter(objectMapper));
converters.add(new JsonToMapConverter(objectMapper));
converters.add(new MapToJsonConverter(objectMapper));

    applicationContext.getBeansOfType(CustomConverter.class).forEach((s, customConverter) -> {
        converters.add(customConverter.getConverter());
    });

    return converters;
}
我有相同的Map转换器,它们工作正常,列表的反序列化器工作正常,但列表的序列化器不工作。错误是

Caused by: io.r2dbc.postgresql.ExceptionFactory$PostgresqlBadGrammarException: column "special_characters" is of type jsonb but expression is of type text[]

有人建议,也许spring已经有了自己的列表转换器(将其转换为 `text[]` 而不是 `jsonb` ),反正我也不知道该怎么办,任何提示都欢迎,谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题