I am trying to insert json into json column using spring webflux with postgres sql. I have created custom converter for reading and writing. The issue is existing json values becomes null, whenever trying to update the record. The new value is inserted as json along with null object as well. Please let me know where i am doing it wrong
@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.
}