https://kotlinlang.org logo
#http4k
Title
# http4k
m

Moritz Platt

10/07/2021, 10:00 AM
I'm struggling to register a custom Gson configuration with my app. It looks as if, when using
body.auto
my Gson config is not used:
Copy code
val auto: CustomerCreateModel = Body.auto<CustomerCreateModel>().toLens().extract(it)
        val manual: CustomerCreateModel = MyGson.mapper.fromJson(it.bodyString(), CustomerCreateModel::class.java)
        assert(auto == manual) //fails
I have the following on my classpath:
Copy code
object MyGson : ConfigurableGson(
    GsonBuilder()
        .registerTypeAdapter(Country::class.java, CountryDeserializer())
        .registerTypeAdapter(Country::class.java, CountrySerializer())
        .setDateFormat("yyyy MM-dd'T'HH:mm:ss.SSS'Z'")
        .asConfigurable()
        .withStandardMappings()
        .done()
)
I'm on a fairly old version of http4k (3.261.0), btw
d

dave

10/07/2021, 10:03 AM
you're probably importing the wrong auto method when referencing the
Body.auto
. If you've got
import org.http4k.format.Gson.auto
in your file then it's the wrong one. you need to import your method instead
m

Moritz Platt

10/07/2021, 10:07 AM
Ah! How subtle! Thanks that was it
d

dave

10/07/2021, 10:09 AM
yeah, it's a bit of an annoyance - the auto import in Idea doesn't pick up the extension method (there is a bug currently open). Suggest creating a function in the meantime which delegates to the correct import and just use that one instead 🙂
m

Moritz Platt

10/07/2021, 10:30 AM
Good idea thanks!
25 Views