Which Boolean Naming Convention Is Right for Java POJOs? Avoid Serialization Pitfalls
This article examines how to correctly define boolean fields in Java POJOs—choosing between success and isSuccess, primitive boolean versus Boolean, and adhering to JavaBeans conventions—to prevent subtle bugs when serializing with fastjson, Jackson, or Gson.
In daily development, boolean fields are often used to indicate whether an RPC request succeeded. This article analyses four ways to declare such fields, discusses the naming choice between success and isSuccess, compares primitive boolean with wrapper Boolean, explains JavaBeans getter/setter rules, and shows how fastjson, Jackson, and Gson handle serialization differently, exposing potential bugs and offering best‑practice recommendations from the Alibaba Java Development Manual.
1. success or isSuccess
Both names are semantically clear, but JavaBeans conventions differ: a primitive boolean generates an isXxx() getter, while a wrapper Boolean generates a getXxx() getter. Using isSuccess as the field name would require a getter named isIsSuccess, which many IDEs simplify to isSuccess, causing mismatches during serialization.
According to the Alibaba Java Development Manual, primitive boolean fields should be named success and use the isSuccess getter, while wrapper Boolean fields follow the getSuccess pattern.
2. Boolean vs boolean
Primitive boolean defaults to false; wrapper Boolean defaults to null. The manual recommends using wrapper types for POJOs and RPC return values because a null can signal an abnormal situation (e.g., a missing rate in a billing system), whereas 0.0 might silently produce wrong calculations.
However, for boolean fields that only represent true/false, the author argues that using boolean and naming the field success avoids the need to handle null and prevents NPEs in client code.
3. Serialization impact
Fastjson and Jackson serialize an object by scanning getters; they treat isSuccess as a property named success. Gson, by contrast, serializes fields directly, preserving the original name. This discrepancy can cause mismatched JSON output and deserialization failures, especially when a JSON string produced by fastjson is parsed by Gson.
When the JSON contains {"success":true}, Gson looks for a field named success, does not find it (the class has isSuccess), and sets the boolean to its default false, leading to silent logic errors in production.
4. Recommended practice
Define the field as success (primitive boolean) and let the getter be isSuccess(). This follows JavaBeans specifications, works consistently across all major JSON libraries, and eliminates the serialization‑deserialization mismatch.
In summary, when exposing boolean results in POJOs or RPC responses, use the success field name, prefer primitive boolean for simple true/false flags, and consider wrapper Boolean only when a null value conveys meaningful information.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
