当前位置: 首页 > 知识库问答 >
问题:

如何使用Uni、Mutiny中的变量

黄扬
2023-03-14

我与Quarkus和Mutiny创建了一个反应式resteasy服务。在POST方法中,我在PostgreSQL表中插入对象,并得到一个Uni

创建方法:

  public static Uni<Long> create(PgPool client, RetailPlace retailPlace) {
        return client.withTransaction(conn -> conn
                .preparedQuery("INSERT INTO retail_place (title) VALUES ($1) RETURNING id")
                .execute(Tuple.of(retailPlace.getRetailPlaceTitle()))
                .onItem().transformToUni(id -> conn.
                        preparedQuery("INSERT INTO retail_place_address (retail_place_id,region_code,city,locality_id,apartment,house,region,street) " +
                                "VALUES ($1,$2,$3,$4,$5,$6,$7,$8) returning retail_place_id")
                        .execute(Tuple.tuple(Arrays.asList(id.iterator().next().getLong("id"), retailPlace.getRetailPlaceAddress().getRegionCode(),
                                retailPlace.getRetailPlaceAddress().getCity(), retailPlace.getRetailPlaceAddress().getLocalityId(),
                                retailPlace.getRetailPlaceAddress().getApartment(), retailPlace.getRetailPlaceAddress().getHouse(),
                                retailPlace.getRetailPlaceAddress().getRegion(), retailPlace.getRetailPlaceAddress().getStreet())))))
                .onItem().transform(pgRowSet -> pgRowSet.iterator().next().getLong("retail_place_id"));
    }

我得到id的长值作为回报。现在,我需要返回一个带有id值的RetailPlaceResponse:

public RetailPlaceResponse(String errorCode, long id, boolean isSuccessful) {
    this.errorCode = errorCode;
    this.id = id;
    this.isSuccessful = isSuccessful;
}

共有1个答案

庄瀚玥
2023-03-14

如果需要成功响应和非成功响应映射到同一个类,可以编写单独的转换。一个代表成功,一个代表失败。

public static Uni<RetailPlaceResponse> create(PgPool client, RetailPlace retailPlace) {
    return client.withTransaction(conn -> conn
            .preparedQuery("INSERT INTO retail_place (title) VALUES ($1) RETURNING id")
            .execute(Tuple.of(retailPlace.getRetailPlaceTitle()))
            .onItem().transformToUni(id -> conn.
                    preparedQuery("INSERT INTO retail_place_address (retail_place_id,region_code,city,locality_id,apartment,house,region,street) " +
                            "VALUES ($1,$2,$3,$4,$5,$6,$7,$8) returning retail_place_id")
                    .execute(Tuple.tuple(Arrays.asList(id.iterator().next().getLong("id"), retailPlace.getRetailPlaceAddress().getRegionCode(),
                            retailPlace.getRetailPlaceAddress().getCity(), retailPlace.getRetailPlaceAddress().getLocalityId(),
                            retailPlace.getRetailPlaceAddress().getApartment(), retailPlace.getRetailPlaceAddress().getHouse(),
                            retailPlace.getRetailPlaceAddress().getRegion(), retailPlace.getRetailPlaceAddress().getStreet())))))
            .onItem().transform(pgRowSet -> new RetailPlaceResponse(null, pgRowSet.iterator().next().getLong("retail_place_id"), true))
            .onFailure().transform(error -> new RetailPlaceResponse(error.toString(), 0, false));
}
 类似资料:
  • 我是反应式编程的新手。请协助在穆特尼实现以下目标。 我有一节DTO课 3种服务方式,一种返回Uni,另一种返回Multi。 我需要编写一个调用上述3个方法的服务方法,应用一些业务验证,将返回值设置为的实例并返回一个

  • Mutiny 是一个社交网络,使用 PHP 编写,可写文章、交友、留言、上传照片和收藏。同时可作为非常简单的项目管理,可跟踪目标、任务和 bug。

  • 最近,我正在从事一个项目,其中我有2个同时进行2个异步调用。由于我与Quarkus合作,我最终尝试利用叛变和垂直。x库。然而,我无法让我的代码与Unis一起工作。在下面的代码中,我可以想象两个Uni都将被调用,返回速度最快的Uni将被返回。然而,当组合uni时,似乎只返回列表中的第一个uni,即使第一个uni需要更长的时间。 由于uniFast应该首先完成,所以下面的代码应该在打印时打印出来。如何

  • 我正在处理一个反应式quarkus后端服务,它执行以下操作。 使用与postgres交互的Hibernate反应式Panache从后端获取记录列表 使用记录的标识符,从另一个远程服务获取数据 我正在使用Mutiny来执行反应式管道。远程服务和数据库集成都以非阻塞方式单独工作。我只是需要帮助写一个连接这些的管道。例如:下面的例子 我被困在处理统一包装列表,然后试图处理列表中的单个项目。要么是Uni

  • 问题内容: 我听说我们可以在CSS中声明/定义变量,并将其像全局变量一样使用: 所以有人知道如何使用它们吗? 问题答案: 为此,您需要使用CSS动态语言的 Less 或 Sass。

  • 问题内容: Maven有一个功能,我在package.json中非常想念。在maven .pom文件中,您可以在父项目中定义变量,并在子项目的pom文件中使用它们。 npm中有类似的东西吗?我们正在构建模块化项目,我想集中定义依赖项版本,并在相应的package.json文件中使用它们。 谢谢 问题答案: 可以引用任何键,对于您所下的每个级别,都可以开头并添加下划线。 例: 名称可以像这样访问:或