Spring Reactive get body JSONObject using ServerRequest
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>
.
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.