Why Enums Should Be Avoided in RPC Interface Parameters and Return Values

The article analyzes a runtime IllegalArgumentException caused by an enum constant added in a downstream library, explains how JSON‑based RPC deserialization fails when the receiving service lacks the new enum value, and recommends using strings instead of enums for RPC interfaces to improve compatibility and maintainability.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Why Enums Should Be Avoided in RPC Interface Parameters and Return Values

In a production environment an IllegalArgumentException was thrown because the enum AType in a downstream library added a new constant P_M that the consuming service had not upgraded to, leading to a deserialization failure during an RPC call.

The issue was reproduced by defining a shared library with an interface AFacadeService returning an AResponse containing an AType field. Service A upgraded the library to include P_M, while Service B kept the old version, causing the RPC framework to attempt Enum.valueOf on a missing constant and throw the exception.

The root cause lies in most RPC frameworks serializing responses to JSON and deserializing them back to Java objects; during deserialization the Enum.valueOf method throws an IllegalArgumentException when the enum constant is absent.

According to the Alibaba Java Development Manual, enums should be used for request parameters but not for return values, reflecting the same compatibility concerns described above.

Extended analysis argues that while enums help validate input and document allowed values, they create tight coupling: any change in a shared library forces all downstream services to upgrade, which is often impractical for large ecosystems with many callers.

The author therefore recommends replacing enum fields in RPC interfaces with plain String types, documenting the allowed values via Javadoc @see references, thus avoiding deserialization errors and reducing dependency constraints.

Overall, the article advises minimizing the use of strong‑typed enums in both request and response payloads of public RPC APIs to achieve better backward compatibility and easier evolution of services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaRPCenumbest practices
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.