繁体   English   中英

WebFlux - 在 Webflux 中读取纯文本请求正文

[英]WebFlux - read plain text request body in Webflux

我使用的是 Java WebFlux 2.5.2 版。 我需要提取纯文本,它作为 POST 请求出现,然后我将不得不处理文本。 如何从 ServerRequest 对象中提取正文?

我正在粘贴我的路由器和处理程序代码。 将图像与显示我尝试提取有效负载的代码的错误的观察窗口一起附加。

在此处输入图片说明

路由器类如下:

package com.test;

import com.test.AppHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.*;

@Configuration
public class AppRouter {

    @Bean
    public RouterFunction<ServerResponse> route(AppHandler appHandler){
        return RouterFunctions
                .route(GET("/").and(accept(MediaType.TEXT_PLAIN)), appHandler::ping)
                .andRoute(POST("/process").and(accept(MediaType.ALL)).and(contentType(MediaType.TEXT_PLAIN)), appHandler::process);
    }

}

Handler类如下:

package com.test;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;



@Component
public class AppHandler {


    public Mono<ServerResponse> process(ServerRequest request){

        //Extract the request body
        Mono<String> responseBody = request.bodyToMono(String.class);
        responseBody.map(s -> s + " -- added on the server side.");

        return ServerResponse.ok()
                .body(responseBody.log(), String.class);

    }

    public Mono<ServerResponse> ping(ServerRequest request){
        return ServerResponse.ok()
                .contentType(MediaType.TEXT_PLAIN)
                .body(Mono.just("PONG").log(), String.class);
    }


}

pom依赖提取

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    ...
<dependencies>

您发布的错误不是来自您发布的代码。 您不允许在反应式应用程序中阻塞,并且您使用了关键字block ,这意味着您将收到IllegalStateException因为阻塞是非法的。

然后在您发布的代码中,问题是您正在破坏链条。

responseBody.map(s -> s + " -- added on the server side.");

这一行只是一个声明,但nothing happens until you subscribe ,并且没有任何内容订阅该行,因此您需要跟进并处理 map 函数的返回。

return responseBody.flatMap(s -> {
    return ServerResponse.ok()
        .body(Mono.just(s + " -- added on the server side."), String.class);
});

当客户端调用您的应用程序时,他们正在订阅,因此您必须确保服务器端的链完好无损,以便我们可以将项目提供给客户端。 但是您通过不处理上面一行中的返回来破坏它,因此什么也没有发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM