Home > Blockchain >  spring json serialization issue
spring json serialization issue

Time:12-31

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"))
}

CodePudding user response:

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.

  • Related