How to dynamic generate Enum type from mysql in Java spring boot application?


In my project,we want manage all REST APIs errorcodes in a Enum type,like the following code:

package com.example.util
public enum ErrorType{
  SUCCESS("0000", "success")
  PARAMS_EMPTY("3001", "params cannot be empty")

The problem we encounter is if we put the class into util package,everytime we add a new error type in business spring boot app,we’ll need to modify,publish and recompile the app and util project.That would be hard to maintance the util package.Basically,we prefer to maintance a relatively stable utility package.

So we are considering if there is a way that we can generate Enum type dynamiclly,we can comfigure the error information in mysql in advance,then we can load them into enum type in application boot procedure.

I’m not sure is this a good idea to dynamic generate enum type in Java,or if there is a better solution for this problem.


Thanks for your reply,we finally decide to give up this idea.Dynamic generate an enum would not help,indeed it will add more complexity to our project,it’s not a common use of enum.
Instead of this,we predefine our main error type likes user_error,system_error and db_error,etc.The specific error information will be processed in the business service.

Answered By – timgallin

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published