0

这是我的代码:

@Path("/hello")
@AllArgsConstructor
public class GreetingResource {

    private final Driver driver;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() {
        return Multi.createFrom().resource(
            driver::rxSession,
            session -> session.readTransaction(tx -> {
                RxResult result = tx.run("MATCH (f:Fruit) RETURN f.name as name ORDER BY f.name");
                return Multi.createFrom().publisher(result.records()).map(record -> record.get("name").asString());
            })
        ).withFinalizer(session -> {
            return Multi.createFrom().publisher(session.close());
        });
    }

}

我收到这两条编译消息:

Type mismatch: cannot convert from Multi<Object> to Uni<String>
Type mismatch: cannot convert from Multi<Object> to Uni<Void>

我不太清楚,因为record.get("name").asString给我一个String...

有任何想法吗?

4

1 回答 1

0

终结器函数必须返回一个Uni<Void>. 在您的代码中,它返回一个Publisher<Object>. 此外,您的方法将返回 aMulti而不是 a Uni

尝试以下方法:

@GET
@Produces(MediaType.TEXT_PLAIN)
public Multi<String> hello() {
    return Multi.createFrom().resource(
            driver::rxSession,
            session -> session.readTransaction(tx -> {
                RxResult result = tx.run(
                  "MATCH (f:Fruit) RETURN f.name as name ORDER BY f.name");
                return Multi.createFrom().publisher(result.records())
                   .map(record -> record.get("name").asString());
            })
    ).withFinalizer(session -> {
        return Uni.createFrom().publisher(session.close());
    });
}
于 2021-11-24T09:39:35.903 回答