Spring Reactive get body JSONObject using ServerRequest

邮差的信 提交于 2020-08-23 03:52:42

问题


i'm spring reactive newbie.

I am trying to use postman to get request information from the server.

First, postman sends information to the server using the post method. Second, we've been working on the server side with the relevant code and getting the request information.

In the following code snippet

I wonder if I can get the JSONObject of the ServerRequest function.

postman body(application/json)

{
    "name": "aaaa",
    "name_order": ["aa", "bb", "cc"],
    "type": "12",
    "query": ""
}

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

java (Handler)

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

回答1:


thank you. Alexander Terekhov

Your answer has been a lot of help in solving the problem.

My Test Code.

RouterFunction = Same as existing code.

Handler

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

and the result in console as provided below :-

==========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======

Happy Coding Thank you.




回答2:


I think you can try to register a kind of 'callback' in the next way:

        return request.bodyToMono(JSONObject.class)
                  .doOnNext(jsonObject -> // testing..)
                  .then(ServerResponse.ok().build());

Also, I noticed that you are casting ServerResponse.ok() to Mono<ServerResponse>. I think it will not cast. Use ServerResponse.ok().build() to make Mono<ServerResponse>.



来源:https://stackoverflow.com/questions/52870517/spring-reactive-get-body-jsonobject-using-serverrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!