Its OK to use public field in Java in this case. Agree?
-
I am defining a "wire format" of a business object. This wire format is going to be serialized to JSON, and sent across the wire to a RESTful API consumers (e.g. JavaScript in HTML page). I experimented with the JAX-RS processing. The following two patterns yield the exact same JSON output: [Pattern A] public class Student { public String firstName; } [Pattern B] public class Student { private String firstNameXYZ; // made internal name a bit weired to emphasize information hiding. public String getFirstName { return this.firstNameXYZ; } public void setFirstName(String firstName) { this.firsNameXYZ = firstName; } } I want to use [Pattern A] because of the following reasons: Unlike regular Java-binding, the client binds to this data via JSON. So the binding is not as strong as Java-to-Java binding, so exposing public fields is not as bad as Java-to-Java binding scenario (where encapsulating with getter/setter is very useful for achieving loose-coupling) This is for wire format. Basically a "struct" with almost no behavior (there could be some translation between Enum and String etc, but no "behavior" in the sense that it causes state change be receiving messages etc.) Because this is for defining data to be consumed via REST, we plan to support "legacy" version as we continue to develop the API. That is: we plan *not* to remove any field. We continue to add new fields and recommend client developers to not to use "obsolete/outdated" fields, but nonetheless we won't remove it. Any input appreciated!
-
Answer:
What you describe here has nothing to do with the fact you're planing a REST WS , or use JSON , really. But,.. it has to do with the fact you're using an OBJECT ORIENTED language. And one of OO core concept , called "encapsulation" , guides you to use pattern b. Good luck.
Pavel PK Kaminsky at Quora Visit the source
Related Q & A:
- How to get the Field Name,Field Length and Field Type?Best solution by Stack Overflow
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Why cannot we use static keyword inside a method in java?Best solution by Stack Overflow
- What is better for the Ipod: Plastic protective case or Silicon Protective case?Best solution by answers.yahoo.com
- What is the best job in the Public Health field?Best solution by onlinemastersinpublichealth.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.