春の反応はServerRequestを使って体JSONObjectを取得します

oddeveloper:

私は春の反応初心者です。

私は、サーバーからのリクエスト情報を取得するために郵便配達を使用しようとしています。

まず、郵便配達は、POSTメソッドを使用してサーバに情報を送信します。第二に、我々は、関連するコードをサーバー側で作業し、要求情報を取得してきました。

次のコードスニペットで

私はServerRequest機能のJSONObjectを得ることができるのだろうか。

郵便配達体(アプリケーション/ 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(ハンドラ)

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

ありがとうございました。アレクサンダーTerekhov

あなたの答えは、問題を解決するために多くの助けとなっています。

私のテストコード。

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

コンソールでの結果の下に提供されます: -

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

コーディングハッピーありがとうございました。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=185132&siteId=1