Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson fails to deserialize 1 field POJO #3085

Closed
WojciechDolata opened this issue Mar 20, 2021 · 2 comments
Closed

Jackson fails to deserialize 1 field POJO #3085

WojciechDolata opened this issue Mar 20, 2021 · 2 comments
Labels
lombok Issue (likely) related to use of Lombok to-evaluate Issue that has been received but not yet evaluated

Comments

@WojciechDolata
Copy link

Describe the bug
When trying to deserialize JSON to POJO it fails when POJO has only one field.

Version information
2.11.4

To Reproduce
POJO:
@AllArgsConstructor //lombok, if i add constructor manually it still fails
public class SubjectRequest {
public final String name;
}

While trying to create this POJO via spring's @requestbody it fails. When i add another parameter, ex public final String a;
jackson works correctly. (creates POJO with {name: 'text', a: null})

Expected behavior
Jackson should be able to deserialize 1 field POJO

Error message

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of ad.school.planner.inner.subject.SubjectRequest (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of ad.school.planner.inner.subject.SubjectRequest (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 5]]

@WojciechDolata WojciechDolata added the to-evaluate Issue that has been received but not yet evaluated label Mar 20, 2021
@cowtowncoder cowtowncoder added the lombok Issue (likely) related to use of Lombok label Mar 20, 2021
@cowtowncoder
Copy link
Member

This is a well-known issue due to ambiguity of the 1-arg constructor case: single-argument could match either:

  1. Delegating case like "string-value" OR
  2. Properties-based case like {"name" : "value"}

and if user does not specify mode with

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) // or Mode.DELEGATING

Jackson will try to guess which one to use. In your case it likely guesses that DELEGATING mode is to be used and expects a String, not Object value.

I think Lombok has a way to get appropriate @JsonCreator (or @ContructorProperties which also implies properties-based approach).

Or... Jackson 2.12 does allow configuration to default to Properties, always. This is probably the best way:

https://cowtowncoder.medium.com/jackson-2-12-most-wanted-3-5-246624e2d3d0

@WojciechDolata
Copy link
Author

Thanks a lot! Adding @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) solved my issue.

kiekk referenced this issue in kiekk/inflearn-java-spring-test Nov 21, 2023
* jackson-databind 쪽 이슈를 보니 이미 잘 알려진 이슈라고 한다. 기본 생성자를 명시적으로 추가하는 방법 외에 @JsonCreator 애노테이션을 사용하는 방법도 가능하다고 한다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lombok Issue (likely) related to use of Lombok to-evaluate Issue that has been received but not yet evaluated
Projects
None yet
Development

No branches or pull requests

2 participants