com.fasterxml.jackson.databind.exc.MismatchedInputException 2.12 for Java record
See original GitHub issueGiven:
Version information
version 2.11.3 which works properly openjdk 15.0.2
public record UserId(String raw){ }
jsonBody -> {"userId": "123456" }
ObjectMapper objectMapper = new ObjectMapper()
objectMapper.readValue(jsonBody, UserId.class)
noThrowsException()
Version information version 2.12 which not works properly openjdk 15.0.2
public record UserId(String raw){ }
jsonBody -> {"userId": "123456" }
ObjectMapper objectMapper = new ObjectMapper()
objectMapper.readValue(jsonBody, UserId.class)
throws `Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `user.model.UserId` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('123456')
Version information version 2.12 which works properly openjdk 15.0.2
public record UserId(String raw) {
@JsonCreator(mode = Mode.DELEGATING)
public UserId {}
}
jsonBody -> {"userId": "123456" }
ObjectMapper objectMapper = new ObjectMapper()
objectMapper.readValue(jsonBody, UserId.class)
noThrowsException()
I am expecting Jackson to serialize String value to UserId record without using extra annotations as before. What can I do to have a pure approach? Maybe some global configuration on object mapper can be set?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
java - Unable to deserialize when using new Record classes
So in short, you can not use Record for mapping the response. EDIT as PSA: Jackson can properly serialize and deserialize records as...
Read more >MismatchedInputException (jackson-databind 2.12.2 API) - javadoc.io
General exception type used as the base class for all JsonMappingException s that are due to input not mapping to target definition; these...
Read more >FasterXML/jackson-databind - Gitter
The exception is com.fasterxml.jackson.databind.exc.MismatchedInputException . I am using Lombok and have tried the annotation @ToString.
Read more >SUSE-SU-2022:1678-1: important: Security update for jackson ...
SUSE Security Update: Security update for jackson-databind, ... + Drop 'jackson-module-scala_2.10' entry (not released for Jackson 2.12 or ...
Read more >Uses of Class com.fasterxml.jackson.databind.introspect ...
Factory method for creating strategy instance for special java.lang.Record type (new in JDK 14). AccessorNamingStrategy, DefaultAccessorNamingStrategy.Provider.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dkubicki Ok. So the reasoning for this change is two-fold:
Record
types: they happen to mostly work by forcing setting of private fields, but this may not work with newer JDKsRecord
s are collection of named properties, the default logic for all cases – 1, 2 or how many properties – is to use “Properties” style, expecting JSON Object, with matching name/value pairs. This differs from POJOs where logic for 1-argument case is much more complex and unpredictableAlthough it would be possible to add a feature in
ConstructorDetector
to also allow overriding 1-argument Record case. But as of now, adding the annotation is way to go.@cowtowncoder in my dependencies tree I have the 2.12.0 version
| | | | +--- com.fasterxml.jackson.core:jackson-core:2.10.3 -> 2.12.0 (*) | | | | +--- com.fasterxml.jackson.module:jackson-module-kotlin:2.10.3 -> 2.12.0 | | | | | +--- com.fasterxml.jackson.core:jackson-databind:2.12.0 (*) | | | | | +--- com.fasterxml.jackson.core:jackson-annotations:2.12.0 (*) | | | | | \--- com.fasterxml.jackson:jackson-bom:2.12.0 (
Thanks for the replay I gonna try your approach.
Update: I’ve tried
2.12.2
then I set up.constructorDetector(ConstructorDetector.USE_DELEGATING)
Unfortunately, it doesn’t work. Still, I have to have@JsonCreator(mode = Mode.DELEGATING)