Polymorphic deserialisation

Viewed 140

I am trying to call an API in spring which has two types of the response structure

Success Response:

{
    "Response": {
        "SubHeader": {
            "requestUUID": "ABC12345678-9x",
            "serviceRequestId": "HI",
            "serviceRequestVersion": "1.0",
            "channelId": "ABC"
        },
        "BodyEncrypted": "OOnpBMVUq6nw/3UesG/L0+lUT0hjOlCYJI064JaAqJvqYYY2bnENE5T/AxOBuoNPUer/W1ioIyDNpj/8cCHzXk5gXF1Cd50gMuGuErlGUogn0Z8hA05q3mygHaf1eb1EiX8qpv1Ke+7I7jw5ds7YsWPuJtXXhhkL0ShrKUimNs0B2OoQxbgH434+JEBvhRa4kcnUvZF68kmgp74k46RUGRbnMUqBEMC5iNMSm0oQgeg="
    }
}

Failure Response:

{
    "httpCode": 403,
    "httpMessage": "Forbidden",
    "errorCode": "B001007",
    "moreInformation": "Unable to fetch details"
}

Feign Client:

fun generateDetails(
            @RequestHeader clientId: String,
            @RequestBody body: RequestDto
    ): ResponseDto

Can anyone please help me with how to make DTO's so that the response gets handled for both cases.I tried using Polymorphism, but it doesn't seem to work. I am confused about using polymorphic deserialization. Basically, I want to make a general response DTO and handle both the scenario.

1 Answers

You can refer nested JSON mapping instead of polymorphism. To map nested json values, refer link here

Related