0

I am unable to get is_secure object attribute in json response, what is wrong with this code ?

@Configuration
class RouterConfiguration( ) {
    @Bean
    fun testRoutes(testHandler: TestHandler) = coRouter {
        GET("/test", testHandler::testFunction)
    }
}
data class TestClass(
    val is_secure: Int? = 1,
    val xyz: String?
)
@Component
class TestHandler{
    suspend fun testFunction(request: ServerRequest): ServerResponse =
        ServerResponse.ok().bodyValueAndAwait(TestClass(1,"abc"))
}

1 Answer 1

2

is prefixed fields (with camelCase or snake_case pattern) are only serialized if they are of type Boolean. You can find more details about it here.

If you wish to keep the is prefix, you may do so by using @get use-site target. Just use @get:JsonProperty("is_secure") on the is_secure field and it should do.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i tried searching multiple place, but did not find anything.
Yes, Agreed that the problem itself in not very obvious looking at it. The main reason for this behaviour is that the serializations are in accordance to Java Beans spec §8.3.2 download.oracle.com/otndocs/jcp/… . According to it, is-getters are valid only for boolean properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.