Default constructor and no setters on data object break serialization
See original GitHub issueI’ve set up a small app with a REST-Service serving Person
objects. Therefore I’m using jackson-jaxrs-json-provider
on version 2.7.6
.
Person
used to have a 3-arg-constructor and getters only and serialisation was working as expected: verbose JSON
Then I added a default constructor to be able to build a client. This broke serialisation: empty JSON
I realised that the serialiser may misses the setters, so I added them. Funny thing, the JSON became leaner: lean JSON
I’ve got three questions:
- Why don’t I get any warnings that the setters are missing?
- Why do I need the setters at all?
- Why is the JSON more verbose when there is no default constructor?
Please let me know, if you need more info 😃
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
java - How are constructors called during serialization and ...
During deserialization the accessible default constructor is called for the first class in the inheritance hierarchy that does not implement ...
Read more >Serialization guidelines | Microsoft Learn
This document provides guidelines to consider when designing an API to be serialized and a summary of the different serialization ...
Read more >Serialization in Java - DigitalOcean
If you notice the java serialization process, it's done automatically. Sometimes we want to obscure the object data to maintain it's integrity.
Read more >Java static code analysis: The non-serializable super class of ...
In order to create the non-serializable ancestor, its no-argument constructor is called. Therefore the non-serializable ancestor of a Serializable class must ...
Read more >Object serialization - R3 Documentation
Increased security by constructing deserialized objects through supported constructors, rather than having data inserted directly into their fields without ...
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
Would it be possible to distill information into a single example class, showing what you did, and what you expected to get.
As to warnings, if there is a JSON property that does not have any way to be set (via visible setter or field, or
@JsonCreator
annotated constructor with@JsonProperty
annotated parameters), an exception will be thrown – unless configuration has been changed to ignore such “unknown” properties. I think newer versions of Spring MVC have changed this default, which can easily hide problems.I have no idea how existence (or not) of default constructor should have any effect on serialization; it should not. Serialization is based on visible getters/fields (either
public
, or explicitly annotated).Sorry, I can’t provide this. Please go ahead and close this issue…