Closed jelle82 closed 1 year ago
This is likely related to the ambiguity between 2 different possible interpretations for 1-argument constructor, to present either
{ "foo": "string" }
123
or "string"
)either of which could map to constructor. This ambiguity does not occur with different number of arguments (since those must be multi-property JSON Objects).
You can use @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
(and Mode.PROPERTIES
) to change interpretation if auto-detected mapping is not what you want.
However: if the class you have is Kotlin class like here, you really should use Kotlin module -- Kotlin classes are not officially supported by core jackson-databind
. They might work (as you've noticed).
I think Kotlin module may use more advanced logic to figure out most likely interpretation -- or maybe it just defaults to what happens to work for you.
Your question
Hi,
Not sure if this is an issue. But it is curious.
When using spring boot, when posting to an end point that takes in a data class that has more than 1 constructor parameter, eg:
data class Person( val foo: String, val bar: String, )
The jackson-module-kotlin is not necessary.
However, if I remove one of the parameter, eg:
data class Person( val foo: String, )
I get following error:
2023-08-20T16:57:51.270+02:00 WARN 1420 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of
de.neozo.java2kotlin.Person(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)]
Adding the jackson-module-kotlin 2.16 seems to solve this error. `