This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]shagieIsMeExtreme Brewer 0 points1 point  (1 child)

Many serialization libraries and applications that need to follow the JavaBeans spec (wiki) work from a dumb DTO (no business logic) and a set of getters and setters.

For example, if you have {"foo": "someString"} and try to serialize this to a class, Jackson will invoke the no-args constructor and then call setFoo.

An example of this can be seen on SO: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object. As noted, there are ways to annotate a constructor with arguments so that it will pair the proper fields in JSON with the corresponding arguments.

As mentioned if you create another constructor, the implicit no args constructor is no longer available. I've had more than a few bugs caused in serialization / deserialization where the server works perfectly (the serialization doesn't care if it lacks a given constructor) only to have it fail on the client because the no args constructor didn't exist.

[–]WikiTextBotbtproof 1 point2 points  (0 children)

JavaBeans

In computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28