Home > Software design >  After hitting Postman API Getting Params empty value as empty in controller by using webflex
After hitting Postman API Getting Params empty value as empty in controller by using webflex

Time:07-28

find postman snapshot enter image description here Below is my controller i'm using @WebfluxService Want to pass key value pair request from the postman while debugging i'm getting empty Value for params

below logger printing null value in my consol.

@PostMapping(value = "/bserviceobligation",consumes = { "application/x-www-form-urlencoded" })
public Mono<SyncBatchResponse> handleSyncBatch(@Valid @RequestParam Map<String, String> params,
        @RequestHeader(value = HEADER_APPLICATION_KEY, required = true) String appKey,
        @RequestHeader(value = HEADER_APPLICATION_ID, required = true) String appId,
        @RequestHeader(value = CORRELATION_ID, required = false) String correlationId) {
    LogHelper.info(this,"Calling SyncBatch for correlationId : " correlationId);
    LogHelper.info(this,"Requested Value1 ("   params.get("Value1")   ')');
    LogHelper.info(this,"Requested Value0 ("   params.get("Value0")   ')');
    LogHelper.info(this,"Requested Value ("   params.size()   ')');
    String unitOfWorkId = null;

    if (correlationId != null && !correlationId.equalsIgnoreCase("")) {         
        if(correlationId.contains(",")){
            correlationId = correlationId.substring(1,correlationId.length());
        }           
        unitOfWorkId = correlationId;
    }else
        unitOfWorkId = UUID.randomUUID().toString();

    LogHelper.info(this,"unitOfWorkId: "   unitOfWorkId);
    boolean isValid = ldapService.checkAuthorization(appId, groupSBSOBLIGATIONHPIACCESS0);
    if (isValid) {
        LogHelper.info(this,"Application authorization against: Enterprise Directory (SUCCESS) ("   appId   ')');
        SyncBatchResponse response = syncService.prepareCombineOrchaRequest(params, appId,appKey, unitOfWorkId);
 return Mono.just(response);
    } else {
        LogHelper.info(this,"Application authorization against: Enterprise Directory (FAILURE) ("   appId   ')');
        SyncBatchResponse response = new SyncBatchResponse();
        response.setStatusCode(LDAP_STATUS_CODE);
        response.setStatusDescription(LDAP_STATUS_DESCRIPTION);
        return Mono.just(response);
    }
}

CodePudding user response:

For recent releases of WebFlux, @RequestParam does not bind the form-data or Key Value pair. In this case use ServerWebExchange instead.

For Minimal Example:

    @PostMapping(value = "/api/query/ask", consumes = {"application/x-www-form-urlencoded"})
    public Mono<String> handleSyncBatch(ServerWebExchange serverWebExchange) {
        return serverWebExchange.getFormData()
                .flatMap(data -> response(data));
    }

    private Mono<? extends String> response(MultiValueMap<String, String> data) {
        log.info("{}", data);
        return Mono.just("Success");
    }

This logs the key value pair sent from postman. The sample cURL:

curl --location --request POST 'http://localhost:10120/api/query/ask' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'val1=asdafasdfasfd' \
--data-urlencode 'val2=["something","som1"]' \
--data-urlencode 'val3={"name":"abc"}' \
--data-urlencode 'val4=[{"name":"xc"},{"age":23}]'

More study: https://github.com/spring-projects/spring-framework/issues/20738

  • Related