m trying to develop a Ktor backend, and Im have so...
# server
s
m trying to develop a Ktor backend, and Im have some trouble with a returning a response. I have a simple /products/all route that (for now) returns a list, but I keep getting is serializeNullable error
Copy code
routing {
    route("/product") {
        get("/all") {
            call.respond(
                status = HttpStatusCode.OK,
                message = service.getAll()
            )
        }
    }
}

class ProductService(private val productRepository: ProductRepository) {

    fun getAll(): List<ProductEntity> {
       return productRepository.getAll()
    }

}

interface ProductRepository {
    fun getAll(): List<ProductEntity>
}

class ProductRepositoryImpl : ProductRepository {

    private val dotenv = dotenv()
    private val database = Database.connect(
        url = "jdbc:mysql://${dotenv["DB_HOST"]}:${dotenv["DB_PORT"]}/${dotenv["DB_NAME"]}${dotenv["DB_TYPE"]}",
        driver = "com.mysql.jdbc.Driver",
        user = dotenv["MYSQL_USER"],
        password = dotenv["MYSQL_PASSWORD"]
    )


    override fun getAll(): List<ProductEntity> {
        return emptyList()
    }
}
java.lang.NoSuchMethodError: 'java.lang.Object io.ktor.serialization.ContentConverter.serializeNullable(io.ktor.http.ContentType, java.nio.charset.Charset, io.ktor.util.reflect.TypeInfo, java.lang.Object, kotlin.coroutines.Continuation)'
i
Hello Stefan! I don't see the whole picture, but it seems that you're trying to return a List of ProductEntity. In order for the data to be transferred over the network, it needs to be "serialized". I know you're trying to send an empty list, but the ProductEntity class still needs to be "serializable". In gradle, import the following:
Copy code
// Serialization
implementation("io.ktor:ktor-server-content-negotiation-jvm")
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
You may need to install the plugin as well. Then, add the following annotation on top of your ProductEntity data class:
Copy code
@Serializable
data class ProductEntity(val id: Int, val name: String)
I hope this helps!
b
might be just a version mismatch, what are the versions of the libraries you're using?