Can't deserialize to pojo class (Kotlin) (RestAssured)

176 Views Asked by At

**Hello! Here is the request to the server **

var stories: Story? =
given().log().all()
.contentType(ContentType.JSON)
.body(body)
.`when`().log().all()
.post(baseURI)
.then().log().all()
.extract().`as`(Story::class.java)

println (Story().title)
println (Story().id)
println (Story().items)

Here is POJO class

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
class Data {
var stories: String? = null
}

class Story() {
var id = 0
var title: String? = null
var description: String? = null
var imageUrl: String? = null
var imageUrlEco: String? = null
var images: String? = null
var style: String? = null
var styleWeb: String? = null
var link: String? = null
var linkWeb: String? = null
var linkEco: String? = null
var buttonText: String? = null
var dateFrom: String? = null
var dateTo: String? = null
var hidden: String? = null
var isAuth: String? = null
var onboardingType: String? = null
var sortOrder = 0
var type: String? = null
var items: String? = null

}

class Item {
var id = 0
var title: String? = null
var description: String? = null
var detailedDescription: String? = null
var imageUrl: String? = null
var imageUrlWeb: String? = null
var style: String? = null
var styleWeb: String? = null
var sortOrder = 0
}

**And here is the response itself **

{
"data": {
"stories": \[
{
"id": 1234,
"title": "AnyText",
"description": "AnyText",
"imageUrl": "AnyText",
"imageUrlEco": AnyText,
"images": \[

                ],
                "style": "AnyText",
                "styleWeb": "AnyText",
                "link": AnyText,
                "linkWeb": AnyText,
                "linkEco": AnyText,
                "buttonText": "AnyText",
                "dateFrom": "2022-11-22T00:00:00Z",
                "dateTo": "2023-01-08T00:00:00Z",
                "hidden": AnyText,
                "isAuth": AnyText,
                "onboardingType": AnyText,
                "sortOrder": 1234,
                "type": "AnyText",
                "items": [
                    {
                        "id": 1234,
                        "title": "AnyText",
                        "description": "AnyText",
                        "detailedDescription": "AnyText",
                        "imageUrl": "AnyText",
                        "imageUrlWeb": "AnyText",
                        "style": "AnyText",
                        "styleWeb": "AnyText",
                        "sortOrder": 1
                    }
                ]
            }
        ]
    }

}

So when I try this:

println (Story().title) println (Story().id) println (Story().items)

The result of running the program is:

null 0 null

What I did wrong?

1

There are 1 best solutions below

2
AndrewL On

I have not used RestAssured so I cannot comment on the syntax/correctness of that, but your error is incorrectly referencing the object you have deserialized from the GET request.

var stories: Story? =

this means assign the result to a variable named stories

Later you write

println (Story().title)

what this means it print the field title from a brand new Story object. You should be doing this:

println (stories.title) 
println (stories.id) 
println (stories.items)

(stories should be named story if it represents one)