Spring Reactive 使用 ServerRequest 获取主体 JSONObject

odd*_*per 8 java spring reactive

我是春季反应新手。

我正在尝试使用邮递员从服务器获取请求信息。

首先,邮递员使用 post 方法向服务器发送信息。其次,我们一直在使用相关代码在服务器端工作并获取请求信息。

在下面的代码片段中

不知道能不能拿到ServerRequest函数的JSONObject。

邮递员身体(应用程序/ json)

{
    "name": "aaaa",
    "name_order": ["aa", "bb", "cc"],
    "type": "12",
    "query": ""
}
Run Code Online (Sandbox Code Playgroud)

java (RouterFunction)

import com.ntels.io.input.handler.RestInHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.function.server.*;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RequestPredicates.PUT;
import static org.springframework.web.reactive.function.server.RequestPredicates.DELETE;

@Configuration
@EnableWebFlux
public class RestConfig implements WebFluxConfigurer {

    @Bean
    public RouterFunction<ServerResponse> routes(RestInHandler restInHandler){
        return RouterFunctions.route(POST("/input/event").
        and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), restInHandler::toRESTInVerticle);
    }
}
Run Code Online (Sandbox Code Playgroud)

java(处理程序)

public Mono<ServerResponse> toRESTInVerticle(ServerRequest serverRequest) {
    String serverRequestUrl = serverRequest.uri().toString();

    System.out.println("RestInHandler test in");
    System.out.println(serverRequest.method());
    System.out.println(serverRequest.headers());
    System.out.println(serverRequest.uri().toString());

    // how can i get the jsonbody using serverrequest

    // testing..

    // Mono<JSONObject> jsonObjectMono = serverRequest.bodyToMono(JSONObject.class);
    // Flux<JSONObject> jsonObjectFlux = serverRequest.bodyToFlux(JSONObject.class);
-> MonoOnErrorResume

    return (Mono<ServerResponse>) ServerResponse.ok();
}
Run Code Online (Sandbox Code Playgroud)

odd*_*per 6

谢谢你。亚历山大·捷列霍夫

您的回答对解决问题有很大帮助。

我的测试代码。

RouterFunction = 与现有代码相同。

处理程序

public Mono<ServerResponse> toRESTInVerticle(ServerRequest serverRequest) {
    String uri = serverRequest.uri().toString();
    String method = serverRequest.methodName();
    String contentType = serverRequest.headers().contentType().get().toString();
    String characterSet = serverRequest.headers().acceptCharset().get(0).toString();
    JSONObject bodyData = serverRequest.bodyToMono(JSONObject.class).toProcessor().peek();

    System.out.println("==========toRESTInVerticle Data Check==========");
    System.out.println(uri);
    System.out.println(method);
    System.out.println(contentType);
    System.out.println(characterSet);
    System.out.println(bodyData);
    System.out.println("======toRESTInVerticle Data Check Complete======");

    return Mono.empty();
}
Run Code Online (Sandbox Code Playgroud)

控制台中的结果如下所示:-

==========toRESTInVerticle Data Check==========
http://localhost:8082/input/event/check
POST
application/json
UTF-8
{"event_type":"12","event_name_order":["aa","bb","cc"],"event_query":"","event_name":"aaaa","init_value":"","init_value_yn":"N","event_descp":"ddd"}
======toRESTInVerticle Data Check Complete======
Run Code Online (Sandbox Code Playgroud)

快乐编码谢谢。


小智 5

我认为您可以尝试以下一种方式注册一种“回调”:

        return request.bodyToMono(JSONObject.class)
                  .doOnNext(jsonObject -> // testing..)
                  .then(ServerResponse.ok().build());
Run Code Online (Sandbox Code Playgroud)

另外,我注意到您正在投射ServerResponse.ok()Mono<ServerResponse>. 我认为它不会投射。使用ServerResponse.ok().build()进行Mono<ServerResponse>