An effective way to use Java Enums in your Spring application

Dario De Santis
Javarevisited
Published in
7 min readMay 26, 2020

--

Hi! Today I want to expose you a problem that during the years has given me some headaches and give a solution to it. In my first years of work, I was so lacking of experience, as it should be, that I used to treat many types of data in the wrong manner.

Consider for example the structured data used in a typological or in a sort of registry “way” : with this term, I mean system data, that doesn’t change with the application status; this is data which can be often used in HTML select option elements (combobox elements with predefined options and values). Well, I used to map this data with the basic Java String type or with the entire model corresponding to all of the columns and the content of the respective database table. Such table could be like this (just for example) :

The problem

If I had an HTML select option for selecting one of these values, I would have mapped a String to contain the code or maybe I would have mapped a model with all the columns : id, code, description and long_description.

The problem I was facing is that many use cases required using only the code and the…

--

--

Dario De Santis
Javarevisited

Software Architect, writing about Java, Spring, Microservices, Kubernetes and Cloud-native programming. Editor for Javarevisited.