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.

Default constructor and no setters on data object break serialization

See original GitHub issue

I’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:

  1. Why don’t I get any warnings that the setters are missing?
  2. Why do I need the setters at all?
  3. Why is the JSON more verbose when there is no default constructor?

Please let me know, if you need more info 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Aug 15, 2016

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

0reactions
PeterWippermanncommented, Oct 4, 2016

Sorry, I can’t provide this. Please go ahead and close this issue…

Read more comments on GitHub >

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

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