gpt4 book ai didi

java - Spring WebFlux : How to access Request Body in HandlerFilterFunction

转载 作者:行者123 更新时间:2023-12-05 07:20:43 26 4
gpt4 key购买 nike

我正在尝试从 WebFlux 的 HandlerFunctionFunction 访问请求正文,但我收到了 java.lang.IllegalStateException: Only one connection receive subscriber allowed.

我想做类似于下面代码块的事情

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);
}
}

我也尝试了 serverRequest.bodyToMono(Player.class).cache(),但没有成功。

更新:添加处理程序和路由器功能

处理函数

@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);
}
}

路由器功能


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

记录器服务


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

有人可以帮助我吗?谢谢

最佳答案

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());
}

}

我找到了一个解决方案,那就是克隆 serverRequest 并将正文设置为新请求

关于java - Spring WebFlux : How to access Request Body in HandlerFilterFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57385092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com