WebFlux 功能端点,如何 return 带有检索到数据的 ServerResponse 而没有不必要的 block/wait?

WebFlux functional endpoint, how to return a ServerResponse with retrieved data and no unnecessary block/wait?

我对响应式代码完全陌生,在观看了一些教程和 youtube 视频之后,我尝试使用功能端点设置一个小型测试应用程序;一个简单的 RouterFunction、RouterHandler 和 Repository。问题是如何 return 从存储库到调用者的 ServerResponse 中的一个对象,而不引起任何不必要的阻塞?

我正在使用 Postman 进行测试。这是我的测试应用程序中有趣的部分:

@Configuration
public class BookRouter {
    @Autowired 
    BookRepositoryImpl bookRepository;

    @Bean
    public RouterFunction<ServerResponse> bookRoutes() {
        BookHandler bookHandler = new BookHandler(bookRepository);
        return RouterFunctions
            .nest(path("/api/books"),
                route(GET("/{group}/{name}").and(accept(ALL)), bookHandler::getBook)
            );
        }
    }

@Repository
public class BookRepositoryImpl implements BookRepository {
    private final ReactiveMongoTemplate mongoTemplate;

    @Autowired
    public BookRepositoryImpl(ReactiveMongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public Mono<Book> findByName(String group, String name) {
        Query query = new Query(Criteria.where("group").is(group).and("name").is(name));
        return mongoTemplate.findOne(query, Book.class);
    }
}

public class BookHandler {
    public Mono<ServerResponse> getBook(ServerRequest request) {
        String group = request.pathVariable("group");
        String name = request.pathVariable("name");

        bookRepository
            .findByName(group, name)
            .subscribe(
                ok -> System.out.println("findByName " + ok), 
                error -> System.err.println("Error: " + error));
        return ServerResponse
            .accepted()
            .contentType(MediaType.TEXT_PLAIN)
            .bodyValue("Request queued");
    }
}

当我有如上所示的代码时,预期的数据会在 subscribe(ok -> ...) 中打印出来,但我还没有想出如何 return ServerResponse 中的此数据。

如果我将 getBook() 中的代码更改为

return setting
    .flatMap(s -> ServerResponse
        .ok()
        .contentType(MediaType.APPLICATION_JSON)
        .bodyValue(s))
    .switchIfEmpty(NOT_FOUND);

returned bodyValue 是空的,虽然我可以看到它是从数据库中检索到的。

非常感谢任何关于我所缺少的建议。

更新

我正在使用 MongoDB Compass 查看和验证数据库的内容。

application.properties 中使用 logging.level.root=DEBUG 启用调试日志记录,因此 Spring 类 在终端 window 中写入一些信息。部分匿名日志如下:

2020-09-05 21:37:02.688 DEBUG 32720 --- [ctor-http-nio-3] o.s.w.r.f.s.s.RouterFunctionMapping      : [96ef6152-1] Mapped to com.sample.book.BookRouter$$Lambda6/0x0000000800540040@3b0bf8e0
2020-09-05 21:37:02.717 DEBUG 32720 --- [ctor-http-nio-3] o.s.d.m.core.ReactiveMongoTemplate       : findOne using query: { "group" : "Thriller", "name" : "The Shining"} fields: Document{{}} for class: class com.sample.book.Book in collection: book
2020-09-05 21:37:02.734 DEBUG 32720 --- [ctor-http-nio-3] o.s.d.m.core.ReactiveMongoTemplate       : findOne using query: { "group" : "Thriller", "name" : "The Shining"} fields: {} in db.collection: book.book
2020-09-05 21:37:02.751 DEBUG 32720 --- [ctor-http-nio-3] org.mongodb.driver.protocol.command      : Sending command '{"find": "book", "filter": {"group": "Thriller", "name": "The Shining"}, "limit": 1, "singleBatch": true, "$db": "book"}' with request id 7 to database book on connection [connectionId{localValue:2, serverValue:217}] to server localhost:27017
2020-09-05 21:37:02.766 DEBUG 32720 --- [ntLoopGroup-3-2] org.mongodb.driver.protocol.command      : Execution of command with request id 7 completed successfully in 16.24 ms on connection [connectionId{localValue:2, serverValue:217}] to server localhost:27017
2020-09-05 21:37:02.837 DEBUG 32720 --- [ntLoopGroup-3-2] o.s.http.codec.json.Jackson2JsonEncoder  : [96ef6152-1] Encoding [_id=5f53692af0a02d3af8a7fed9, group=Thriller, name=The Shining, value=in]]
2020-09-05 21:37:02.853 DEBUG 32720 --- [ctor-http-nio-3] r.n.http.server.HttpServerOperations     : [id: 0x96ef6152, L:/0:0:0:0:0:0:0:1:8088 - R:/0:0:0:0:0:0:0:1:50248] Decreasing pending responses, now 0
2020-09-05 21:37:02.879 DEBUG 32720 --- [ctor-http-nio-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [96ef6152-1] Completed 200 OK
2020-09-05 21:37:02.905 DEBUG 32720 --- [ctor-http-nio-3] r.n.http.server.HttpServerOperations     : [id: 0x96ef6152, L:/0:0:0:0:0:0:0:1:8088 - R:/0:0:0:0:0:0:0:1:50248] Last HTTP response frame
2020-09-05 21:37:02.931 DEBUG 32720 --- [ctor-http-nio-3] r.n.http.server.HttpServerOperations     : [id: 0x96ef6152, L:/0:0:0:0:0:0:0:1:8088 - R:/0:0:0:0:0:0:0:1:50248] Last HTTP packet was sent, terminating the channel

我发现了问题。我忘记在持有 @DocumentBook class 中实现 getter。我很惊讶他们丢失时没有错误消息或警告。

我一插入它们,结果就按预期从这段代码返回:

return setting
    .flatMap(s -> ServerResponse
        .ok()
        .contentType(MediaType.APPLICATION_JSON)
        .bodyValue(s))
    .switchIfEmpty(NOT_FOUND);

修复后返回给 Postman 的数据如下:

{
    "_id": "5f53692af0a02d3af8a7fed9",
    "group": "Thrillers",
    "name": "The Shining",
    "value": "in"
}

感谢@caco3 帮我找到问题!

这是我的更新 Book.java

@Document
@CompoundIndex(name = "group-name", def = "{'group':1, 'name':1}", unique = true) // Requires auto-index-creation in application.properties
public class Book {
    @Id
    private String _id;
    private String group;
    private String name;
    private String value;

    public Book() {
    }

    public Book(String group, String name, String value) {
        this.group = group;
        this.name = name;
        this.value = value;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder('[');
        s.append("_id=").append(_id);
        s.append(", group=").append(group);
        s.append(", name=").append(name);
        s.append(", value=").append(value);
        s.append(']');

        return s.toString();
    }

    public String get_id() {
        return _id;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}