Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring r2dbc custom Json converter not working

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.

}
like image 966
user1111880 Avatar asked Jan 30 '26 08:01

user1111880


1 Answers

I know my answer is late, but I've just encounter the same issue as yours. The initial solution is almost right except that objectMapper.readValue(json.toString(), SeasonsJsonDto[].class)

json.toString(): will result in something like JsonByteArrayInput{{"id": 1, "text": "Some text", ...}} You should use instead json.asString() inside your objectMapper.readValue(...). The resultant String will be {"id": 1, "text": "Some text", ...}.

Your converter should then looks like that:

@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 List.of(objectMapper.readValue(json.asString(), SeasonsJsonDto[].class));
        
    } catch (Exception e) {
        log.error("Problem while parsing JSON: {}", json, e);
    }
    return new ArrayList<>();
}
like image 173
matico Avatar answered Jan 31 '26 21:01

matico