0

我有一个如下的 DTO:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import reactor.core.publisher.Flux;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class InternetPackageDto {
    private String id;

    private String name;

    private String termsAndConditions;

    private String price;

    private Flux<String> packageAttributes;

    private Flux<String> extras;
}

和一个数据库对象如下:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import reactor.core.publisher.Flux;

@Document("internet_packages")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class InternetPackage {
    @Id
    private String id;

    private String name;

    private String termsAndConditions;

    private String price;

    private Flux<StoreableAttribute> attributes;

    private Flux<StoreableAttribute> extras;
}

StorableAttribute 数据库模型如下:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document("package_attributes")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StoreableAttribute {
    @Id
    private String id;

    private String name;

    private String description;
}

在数据对象上,字段:Flux<StoreableAttribute> attributesFlux<StoreableAttribute> extras与包对象一起存储在单独的集合中。并由映射器处理如下:

 public InternetPackage fromDto(InternetPackageDto dto) {
        var internetPackage = new InternetPackage();

        internetPackage.setName(dto.getName());
        internetPackage.setPrice(dto.getPrice());
        internetPackage.setId(dto.getId());
        internetPackage.setExtras(this.resolePackageExtras(dto));
        internetPackage.setAttributes(this.resolePackageAttributes(dto));

        return internetPackage;
    }

  private Flux<StoreableAttribute> resolePackageExtras(InternetPackageDto dto) {
        return this.storeableAttributeService.resolveAttributes(dto.getExtras());
    }

对于额外的属性也类似。

一个简单的控制器方法如下:

    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes =  MediaType.APPLICATION_JSON_VALUE)
    public Mono<InternetPackageDto> update(@RequestBody InternetPackageDto incomingPackageDto) {
        return this.packageService
                .updatePackage(this.dtoMapper.fromDto(incomingPackageDto))
                .map(this.dtoMapper::toDto);
    }

当我发出帖子请求时,我收到一条错误消息

org.springframework.core.codec.CodecException: Type definition error: [simple type, class reactor.core.publisher.Flux]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `reactor.core.publisher.Flux` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (io.netty.buffer.ByteBufInputStream); line: 2, column: 13] (through reference chain: com.example.api.dto.InternetPackageDto["extras"])

更多信息:

  1. 我将该类InternetPackageDto用作请求对象和响应对象。
  2. 我正在使用Flux<String>,而不是List<String>因为我不确定是否对列表进行阻塞解析是个好主意。
  3. 属性分别存储和管理。
  4. 以及在更新或插入软件包期间;如果包含新的额外或属性,则 db 中的属性集合将通过插入新的传入额外和属性来更新。

看起来我可能犯了一个愚蠢的错误,因为我找不到关于这个问题的太多信息,或者我做错了。

任何帮助将不胜感激。

4

1 回答 1

0

我认为你应该这样做

public Mono<InternetPackageDto> toDto(InternetPackage entity) {
    var internetPackage = new InternetPackageDto();

    internetPackage.setName(entity.getName());
    internetPackage.setPrice(entity.getPrice());
    internetPackage.setId(entity.getId());


    return Mono.zip(Mono.just(internetPackage), entity.getExtras().collectList(), entity.getAttributes().collectList())
            .flatMap(tu->{
                var dto = tu.getT1();
                dto.setExtras(tu.getT2()); //To make it work in my local i made entity.getAttributes() as Flux<String> so here you will probably need to use .stream().map(dbItem->dbItem.getPropertyName())
                dto.setPackageAttributes(tu.getT2());
                return Mono.just(dto);
            });
}
于 2021-05-13T23:21:42.397 回答