Beyond Strings - Leveraging Strong Typing for IDs in Java Apps
In a typical Java application of significant size, numerous domain objects are present,
each often featuring identifiers. These identifiers are commonly defined as Strings, Integers,
or similar data types within many codebases. Moreover, there might be additional alternative
identifiers like account numbers or social security numbers. The challenge arises when interface
methods are exposed, requiring these identifiers as parameters. Without strong typing,
it becomes ambiguous which identifier should be passed in a given method call.
Hence, it is crucial to strong-type these identifiers. Additionally, when accepting and providing
JSON representations of domain objects, it’s vital to serialize these identifiers in their raw form.
Here’s a possible solution:
In order to accept strong types as path variables in Spring based REST controllers you need
to implement and register a ConverterFactory: