java Spring r2dbc自定义Json转换器不工作

qlvxas9a  于 2023-06-28  发布在  Java
关注(0)|答案(1)|浏览(149)

我正在尝试使用spring webflux和postgres sql将json插入json列。我已经创建了自定义转换器的阅读和写作。问题是现有的json值变为null,每当尝试更新记录时。新的值也作为json沿着null对象一起插入。请让我知道我哪里做错了

@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.

}
bzzcjhmw

bzzcjhmw1#

我想这里的答案可能是一个提示。
https://github.com/spring-projects/spring-data-r2dbc/issues/298#issuecomment-586965017
如何 Package 一个列表<?>在一个外部类?我想是因为Java的泛型类型擦除,所以找不到确切的转换器。

相关问题