我正在尝试使用带有 postgres sql 的 spring webflux 将 json 插入 json 列。我创建了用于读写的自定义转换器。问题是,无论何时尝试更新记录,现有的 json 值都会变为空。新值也与空对象一起作为 json 插入。请让我知道我在哪里做错了
@Configuration
@AllArgsConstructor
@EnableR2dbcRepositories("com.gohunt.hd.hunts.entity")
public class ReactivePostgresConfig extends AbstractR2dbcConfiguration {
private final ObjectMapper objectMapper;
@Bean
@Override
public R2dbcCustomConversions r2dbcCustomConversions() {
System.out.println("r2dbcCustomConversions ++++++++++++++++++");
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new JsonToMapConverter(objectMapper));
converters.add(new MapToJsonConverter(objectMapper));
return new R2dbcCustomConversions(getStoreConversions(), converters);
}
@Override
public ConnectionFactory connectionFactory() {
// TODO Auto-generated method stub
return null; // configured via properties file
}
@Slf4j
@ReadingConverter
@AllArgsConstructor
public class JsonToMapConverter implements Converter<Json, List<SeasonsJsonDto>> {
private final ObjectMapper objectMapper;
@Override
public List<SeasonsJsonDto> convert(Json json) {
try {
System.out.println("json +++++++++++++ "+json);
//return AppJsonUtil.parseMessageAsList(json.toString(), SeasonsJsonDto.class);
return Arrays.asList(objectMapper.readValue(json.toString(), SeasonsJsonDto[].class));
} catch (Exception e) {
log.error("Problem while parsing JSON: {}", json, e);
}
return new ArrayList<SeasonsJsonDto>();
}
}
@Slf4j
@WritingConverter
@AllArgsConstructor
public class MapToJsonConverter implements Converter<List<SeasonsJsonDto>, Json> {
private final ObjectMapper objectMapper;
@Override
public Json convert(List<SeasonsJsonDto> source) {
try {
return Json.of(objectMapper.writeValueAsString(source));
} catch (JsonProcessingException e) {
log.error("Error occurred while serializing map to JSON: {}", source, e);
}
return Json.of("");
}
}
@Table("aaaa")
@Data
public class HuntsDetail extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column("id")
private Long id;
@Column("hunter_id")
private Long hunterId;
@Column("hunts_collection_id")
private String huntCollectionId;
@Column("folder_id")
private Long folderId;
@Column("hunts_coverimgUrl")
private String huntsCoverImgUrl;
@Column("seasons_dtl")
private List<SeasonsJsonDto> seasonsDtl; // json datatype in DB using above converters to get it as List or json while reading/writing.
}