Quarkus 未找到序列化程序 class io.smallrye.mutiny.operators.uni.builders.UniCreateFromPublisher 未发现创建 BeanSerializer 的属性

Quarkus No serializer found class io.smallrye.mutiny.operators.uni.builders.UniCreateFromPublisher no properties discovered to create BeanSerializer

我正在使用 Mongo Panache 学习 Quarkus Reactive,当我尝试添加元数据和分页对象时,出现错误: “错误 [io.qu.re.re.ja.ru.ma.NativeInvalidDefinitionExceptionMapper] (executor-thread-0) com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 没有找到 class io.smallrye.mutiny.operators.uni.builders.UniCreateFromPublisher 的序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:com.Entites.BrandResponse["data"])"。 控制器:

@Path("/brand/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RegisterForReflection
public class brand {
  private static Logger LOG = LoggerFactory.getLogger(brand.class);

  @Inject BrandRepository br;

  @GET
  public Response list(
      @DefaultValue("0") @QueryParam("page") Integer page,
      @DefaultValue("10") @QueryParam("page_size") Integer page_size) throws JsonProcessingException {
    if (page != null && page >= 1) {
      page--;
    } else {
      page = 0;
    }
    Uni<List<ReactivePanacheMongoEntityBase>> brands = br.listBrands(page,page_size);
    return Response.ok((new BrandResponse(new Metadata("ok",200,"ok"),brands,new Pagination(3,page,page_size)))).build();
  }
}

存储库

@ApplicationScoped
@RegisterForReflection
public class BrandRepository implements ReactivePanacheMongoRepositoryBase<Brands, Integer> {

  public Uni<List<ReactivePanacheMongoEntityBase>> listBrands(Integer page, Integer page_size) {
    return Brands.findAll(Sort.by("name").ascending()).page(Page.of(page, page_size)).list();
  }
}

实体

@Data
@RegisterForReflection
@MongoEntity(collection = "brand")
public class Brands extends ReactivePanacheMongoEntityBase {

    @BsonId
    public ObjectId id;
    @JsonProperty("name")
    @BsonProperty("name")
    public String name;
}

实体

@RegisterForReflection
@Data
public class BrandResponse {

    @JsonProperty("metadata")
    public Metadata metadata;
    @JsonProperty("data")
    public Uni<List<ReactivePanacheMongoEntityBase>> data;
    @JsonProperty("pagination")
    public Pagination pagination;

  public BrandResponse(Metadata metadata, Uni<List<ReactivePanacheMongoEntityBase>> data, Pagination pagination) {
    this.metadata = metadata;
    this.data = data;
    this.pagination = pagination;
        }
}

实体

@RegisterForReflection
@Data
public class Metadata {

    @JsonProperty("status")
    public String status;
    @JsonProperty("http_code")
    public Integer httpCode;
    @JsonProperty("date_time")
    public Date dateTime;
    @JsonProperty("message")
    public String message;

    public Metadata(String status, Integer httpCode, String message) {
        this.status = status;
        this.httpCode = httpCode;
        this.dateTime = new Date();
        this.message = message;
    }
}

实体

@RegisterForReflection
@Data
public class Pagination{

    @JsonProperty("total_count")
    public Integer totalCount;
    @JsonProperty("page")
    public Integer page;
    @JsonProperty("page_size")
    public Integer pageSize;

    public Pagination(Integer totalCount, Integer page, Integer pageSize) {
        this.totalCount = totalCount;
        this.page = page;
        this.pageSize = pageSize;
    }
}

感谢您的帮助

当 returning 某些异步数据时,您不能让任何字段为异步类型(在本例中为 Uni)。 所以本质上你需要将 Uni<X> 转换为 Uni<Y> 和 return 那个。

在您的情况下,您需要将 BrandResponse 的字段 data 设为 List<ReactivePanacheMongoEntityBase> 而不是 Uni<List<ReactivePanacheMongoEntityBase>>.

完成后,您可以使用类似以下内容创建 BrandResponse

Uni<List<ReactivePanacheMongoEntityBase>> brands = br.listBrands(page,page_size);
Uni<BrandResponse> brandResponse = brands.onItem().transform(b -> new BrandResponse(new Metadata("ok",200,"ok"),b,new Pagination(3,page,page_size)))

既然你有了 Uni,你应该将你的 list JAX-RS 资源方法直接转换为 return Uni<BrandResponse>,或者替换 return javax.ws.rs.core.Responseorg.jboss.resteasy.reactive.RestResponse.

的类型