简体   繁体   English

Spring WebFlux:如何在 HandlerFilterFunction 中访问请求正文

[英]Spring WebFlux: How to access Request Body in HandlerFilterFunction

I'm trying to access the request body from WebFlux's HandlerFunctionFunction but I am getting java.lang.IllegalStateException: Only one connection receive subscriber allowed.我正在尝试从 WebFlux 的HandlerFunctionFunction访问请求正文,但我得到java.lang.IllegalStateException: Only one connection receive subscriber allowed. . .

I want to do something similar to below code block我想做类似于下面的代码块的事情

public class ExampleHandlerFilterFunction 
  implements HandlerFilterFunction<ServerResponse, ServerResponse> {

    @Override
    public Mono<ServerResponse> filter(ServerRequest serverRequest,
      HandlerFunction<ServerResponse> handlerFunction) {
        if (serverRequest.pathVariable("name").equalsIgnoreCase("test")) {
            return serverRequest.bodyToMono(Player.class)
             .doOnNext(loggerService :: logAndDoSomethingElse)
             .then(handlerFunction.handle(serverRequest);
        }
        return handlerFunction.handle(serverRequest);
    }
}

I tried serverRequest.bodyToMono(Player.class).cache() too, but did not work.我也试过serverRequest.bodyToMono(Player.class).cache() ,但没有用。

Update: Adding handler and router functions更新:添加处理程序和路由器功能

Handler Function处理器 Function

@Component
public class PlayerHandler {

    @Autowired
    private final playerRepository;

    public PlayerHandler(PlayerRepository palyerRepository) {

       this.palyerRepository = playerRepository;
    }

    public Mono<ServerResponse> savePlayer(ServerRequest request) {

        Mono<String> id = request.bodyToMono(Player.class)
        .map(playerRepository::save)
        .map(Player::getId);

        return ok().body(id, String.class);
    }
}

Router function路由器 function


@Bean
public RouterFunction<ServerResponse> route(PlayerHandler playerHandler) {
    return RouterFunctions
      .route(POST("/players/"), playerHandler::save)
      .filter(new ExampleHandlerFilterFunction());
}

Logger service记录器服务


public Mono<Void> T logAndDoSomethingElse(T t){
    ---- auditing business logic----
    return loggerRepository.save(asJsonb);
}

Can someone help me?有人能帮我吗? Thanks谢谢

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFilterFunction;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Component
public class FundsAuthorizationFilter implements HandlerFilterFunction<ServerResponse, ServerResponse> {

    @Override
    public Mono<ServerResponse> filter(ServerRequest request, HandlerFunction<ServerResponse> handlerFunction) {
        String block = request.bodyToMono(String.class).block();
        JSONObject jsonObj = new JSONObject(block);
        ServerRequest.Builder newRequestBuilder = ServerRequest.from(request);
        newRequestBuilder.body(block);

        return handlerFunction.handle(newRequestBuilder.build());
    }

}

I found a solution for this, that is clone the serverRequest and set the body to the new request我找到了一个解决方案,即克隆 serverRequest 并将正文设置为新请求

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

相关问题 如何在 spring Webflux Java 中记录请求正文 - How to log request body in spring Webflux Java 如何从 Spring WebFlux 反应式中的 ServerRequest 对象获取请求正文? - How to get request body from ServerRequest object in Spring WebFlux reactive? 如何在将响应返回给调用者时注销对 Spring WebFlux WebClient 请求的失败响应的正文? - How do I log out the body of a failed response to a Spring WebFlux WebClient request while returning the response to the caller? 从 spring webflux 中的 post 请求打印处理程序中的正文 - Print body in handler from post request in spring webflux 弹簧 WebFlux。 如何使用@RequestBody 注释以两种不同格式获取请求正文? - Spring WebFlux. How to get the request body in two different formats using @RequestBody annotation? 如何将请求对象转换为Spring Webflux模型 - How to convert request object to model Spring Webflux 如何访问 Spring WebFlux @ExceptionHandler 中的 RequestBody? - How to access the RequestBody in a Spring WebFlux @ExceptionHandler? WebFlux - 在 Webflux 中读取纯文本请求正文 - WebFlux - read plain text request body in Webflux 如何开启 Spring WebFlux 的访问日志? - How to turn on the access log for Spring WebFlux? 如何在 Spring WebFlux 的响应体中流式传输二进制数据 - How to stream binary data in a response body in Spring WebFlux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM