How to add Kotlin support for Google Java Cloud Endpoints
How to add Kotlin support for Google Java Cloud Endpoints
I'm trying to use Kotlin for Google Java Cloud Endpoints, but I'm getting the following Exception deserializing Kotlin data classes
INFO: exception occurred while calling backend method
com.google.api.server.spi.response.BadRequestException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of my.package.Foo: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: UNKNOWN; line: -1, column: -1]
at com.google.api.server.spi.request.RestServletRequestParamReader.read(RestServletRequestParamReader.java:128)
at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:349)
at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:119)
at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:50)
at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
...
My Kotlin files...
// file: Foo.kt
data class Foo(val bar: String)
// file: MyEndpoint.kt
// My GET works fine as Serialization Does work
@ApiMethod(name = "getFoo",
description = "testing get",
path = "getFoo",
httpMethod = ApiMethod.HttpMethod.GET)
fun getFoo(): Foo
val myFoo = Foo("myBar")
return myFoo
// My Post doesn't work, de-serialization is broken
@ApiMethod(name = "postFoo",
description = "testing post",
path = "postFoo",
httpMethod = ApiMethod.HttpMethod.POST)
fun postFoo(myFoo: Foo): Foo
val postFoo = Foo(myFoo.bar)
return postFoo
I can see that I need to add the KotlinModule to the Jackson ObjectMapper
// file: build.gradle
compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.4")
// adding this will fix the problem, but Where??
objectMapper.registerModule(new KotlinModule());
Where do I manupulate the 'Global' ObjectMapper for Java Endpoints?
I can see that I could add a Transformer to either the @Api or the data classes themselves.
If I add it to the @Api, I don't appear to get any type information, so that's out
Adding it to the data classes involves me making another Transformer for Every data class. Which
seems it would work, but adds A LOT of cruft.
Is there an elegant solution for setting/modifying the default ObjectMapper?
0
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Issue created at github.com/cloudendpoints/endpoints-java/issues/163
– aaronvargas
Sep 10 '18 at 17:10