'How to use Json string as input parameter for post call with WebClient

I'm struggling with an issue of WebClient post request I'm trying to use a jsonString as an input (which uses a RequestDto)

Here is my RequestDto

 class PaymentStartReqestDto implements Serializable{

    @JsonProperty("POSKey")
    String pOSKey
    @JsonProperty("PaymentType")
    String paymentType
    @JsonProperty("PaymentWindow")
    String paymentWindow
    @JsonProperty("GuestCheckout")
    String guestCheckout
    @JsonProperty("FundingSources")
    List<String> fundingSources
    //TODO Ezt a bolt fogja generalni a hivas szambol valahogy
    @JsonProperty("PaymentRequestId")
    String paymentRequestId
    //generált a shop által
    @JsonProperty("OrderNumber")
    String orderNumber
    @JsonProperty("PayerHint") //TODO EZ AZ EMAIL CIM LESZ
    String payerHint
    @JsonProperty("ShippingAddress")
    ShippingAddressDto shippingAddress
    @JsonProperty("RedirectUrl")
    String redirectUrl
    @JsonProperty("CallbackUrl")
    String callbackUrl
    @JsonProperty("Locale")
    String locale
    @JsonProperty("Currency")
    String currency
    @JsonProperty("Transactions")
    List<PaymentTransactionDto> transactions

    static class ShippingAddressDto implements Serializable{

        @JsonProperty("Country")
        String country
        @JsonProperty("City")
        String city
        @JsonProperty("Zip")
        String zip
        @JsonProperty("Street")
        String street
        @JsonProperty("Street2")
        String street2
        @JsonProperty("FullName")
        String fullName
        @JsonProperty("Phone")
        String phone
    }

    static class PaymentTransactionDto implements Serializable{

        @JsonProperty("POSTransactionId")
        String pOSTransactionId
        @JsonProperty("Payee")
        String payee
        @JsonProperty("Total")
        String total
        @JsonProperty("Items")
        List<Item> items
    }

    static class Item implements Serializable {

        Item(String name, String description, int quantity, String unit, int unitPrice, int itemTotal, String sKU) {
            this.name = name
            this.description = description
            this.quantity = quantity
            this.unit = unit
            this.unitPrice = unitPrice
            this.itemTotal = itemTotal
            this.sKU = sKU
        }

        @JsonProperty("Name")
        String name
        @JsonProperty("Description")
        String description
        @JsonProperty("Quantity")
        int quantity
        @JsonProperty("Unit")
        String unit
        @JsonProperty("UnitPrice")
        int unitPrice
        @JsonProperty("ItemTotal")
        int itemTotal
        @JsonProperty("SKU")
        String sKU
    }
    }

here is my ResponseDto

class PaymentStartResponseDto implements Serializable{

    @JsonProperty("PaymentId")
    String paymentId

    @JsonProperty("PaymentRequestId")
    String paymentRequestId

    @JsonProperty("Status")
    String status

    @JsonProperty("QRUrl")
    String qRUrl

    @JsonProperty("Transactions")
    List<ProcessedTransactionDto> processedTransactions

    @JsonProperty("GatewayUrl")
    String gatewayUrl

    @JsonProperty("CallbackUrl")
    String callbackUrl

    @JsonProperty("RedirectUrl")
    String redirectUrl

    @JsonProperty("ThreeDSAuthClientData")
    String threeDSAuthClientData

    @JsonProperty("TraceId")
    String traceId

    static class ProcessedTransactionDto implements Serializable{

        @JsonProperty("POSTransactionId")
        String pOSTransactionId
        @JsonProperty("TransactionId")
        String transactionId
        @JsonProperty("Status")
        String status
        @JsonProperty("Currency")
        String currency
        @JsonProperty("TransactionTime")
        String transactionTime
    }
}

this is the method where I create the DTO

        PaymentStartReqestDto requestDto = new PaymentStartReqestDto()
        requestDto.pOSKey = ApplicationProperties.instance.barionPosKey
        requestDto.paymentType = "Immediate"
        requestDto.paymentWindow = "00:30:00"
        requestDto.guestCheckout = "True"
        requestDto.fundingSources = Arrays.asList("All")
        requestDto.paymentRequestId = "payment-26"
        requestDto.orderNumber = "order-26"
        requestDto.payerHint = "[email protected]"
        PaymentStartReqestDto.ShippingAddressDto shippingAddressDto = new PaymentStartReqestDto.ShippingAddressDto()
        shippingAddressDto.country = "HU"
        shippingAddressDto.city = "Budapest"
        shippingAddressDto.zip = "1038"
        shippingAddressDto.street = "Vasút sor 4"
        shippingAddressDto.street2 =""
        shippingAddressDto.fullName = "Zsíros Bence"
        shippingAddressDto.phone = "06707777777"
        requestDto.shippingAddress = shippingAddressDto
        requestDto.redirectUrl = "https://google.com"
        requestDto.callbackUrl = "https//google.com"
        requestDto.locale = "hu-HU"
        requestDto.currency = "HUF"
        PaymentStartReqestDto.PaymentTransactionDto transactionDto = new PaymentStartReqestDto.PaymentTransactionDto()
        transactionDto.pOSTransactionId = "tr-26"
        transactionDto.payee = "[email protected]"
        transactionDto.total = "400"

        PaymentStartReqestDto.Item item = new PaymentStartReqestDto.Item("Digital Camera", "Canon D500", 1, "pcs", 300,300, "cn-d500-fxc3")
        transactionDto.items = new ArrayList<PaymentStartReqestDto.Item>()
        transactionDto.items.add(item)
        requestDto.transactions = new ArrayList<PaymentStartReqestDto.PaymentTransactionDto>()
        requestDto.transactions.add(transactionDto)
        return requestDto
    }

And here is my code

         WebClient barionClient = WebClient.create(ApplicationProperties.instance.barionBaseUrl)

        PaymentStartReqestDto reqestDto = createPaymentRequestData()
        String requestJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(reqestDto)
        String goodRequestJson = "{\n" +
                "   \"POSKey\":\"b8f0189637004869aba00e565ce1ed02\",\n" +
                "   \"PaymentType\":\"Immediate\",\n" +
                "   \"PaymentWindow\":\"00:30:00\",\n" +
                "   \"GuestCheckout\":\"True\",\n" +
                "   \"FundingSources\":[\n" +
                "      \"All\"\n" +
                "   ],\n" +
                "   \"PaymentRequestId\":\"payment-26\",\n" +
                "   \"OrderNumber\":\"order-26\",\n" +
                "   \"PayerHint\":\"[email protected]\",\n" +
                "   \"ShippingAddress\":{\n" +
                "      \"Country\":\"HU\",\n" +
                "      \"City\":\"Budapest\",\n" +
                "      \"Zip\":\"1038\",\n" +
                "      \"Street\":\"Vasút sor 4\",\n" +
                "      \"Street2\":\"\",\n" +
                "      \"FullName\":\"John Doe\",\n" +
                "      \"Phone\":\"06707777777\"\n" +
                "   },\n" +
                "   \"RedirectUrl\":\"https://google.com\",\n" +
                "   \"CallbackUrl\":\"https://googlecm\",\n" +
                "   \"Locale\":\"hu-HU\",\n" +
                "   \"Currency\":\"HUF\",\n" +
                "   \"Transactions\":[\n" +
                "      {\n" +
                "         \"POSTransactionId\":\"tr-25\",\n" +
                "         \"Payee\":\"[email protected]\",\n" +
                "         \"Total\":400,\n" +
                "         \"Items\":[\n" +
                "            {\n" +
                "               \"Name\":\"Digital Camera\",\n" +
                "               \"Description\":\" " + "CANON-500" + " \",\n" +
                "               \"Quantity\":1,\n" +
                "               \"Unit\":\"pcs\",\n" +
                "               \"UnitPrice\":300,\n" +
                "               \"ItemTotal\":300,\n" +
                "               \"SKU\":\"cn-d500-fxc3\"\n" +
                "            }\n" +
                "         ]\n" +
                "      }\n" +
                "   ]\n" +
                "}"
        PaymentStartResponseDto response = barionClient
                .post()
                .uri(ApplicationProperties.instance.barionPaymentStartUri)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .bodyValue(requestJson)
        .retrieve()
        .bodyToMono(PaymentStartResponseDto.class)
        .block()

If i use the goodRequestJson I can map the result to the PaymentStartResponseDto, But if I'm using the requestJson - which comes from the objectMapper (which seems to be similar to goodRequestJson)

i got

   Suppressed: java.lang.Exception: #block terminated with an error
       at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99) ~[reactor-core-3.4.12.jar:3.4.12]
       at reactor.core.publisher.Mono.block(Mono.java:1707) ~[reactor-core-3.4.12.jar:3.4.12]

if I remove the block call, I got

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'MonoFlatMap' with class 'reactor.core.publisher.MonoFlatMap' to class 'hu.tavterapia.barion.dto.PaymentStartResponseDto'
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:408) ~[groovy-3.0.9.jar:3.0.9]
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319) ~[groovy-3.0.9.jar:3.0.9]
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:243) ~[groovy-3.0.9.jar:3.0.9]
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:615) ~[groovy-3.0.9.jar:3.0.9]

could anyone help?

Thanks, Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source