question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

com.fasterxml.jackson.databind.exc.MismatchedInputException 2.12 for Java record

See original GitHub issue

Given:

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:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Apr 2, 2021

@dkubicki Ok. So the reasoning for this change is two-fold:

  1. Before 2.12, there was no planned support for Record types: they happen to mostly work by forcing setting of private fields, but this may not work with newer JDKs
  2. With 2.12 support was added and since conceptually Records 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 unpredictable

Although 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.

1reaction
dkubickicommented, Apr 2, 2021

@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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found