Best practice to look up Java Enum

前端 未结 10 839
甜味超标
甜味超标 2021-02-01 13:17

We have a REST API where clients can supply parameters representing values defined on the server in Java Enums.

So we can provide a descriptive error, we add this

10条回答
  •  无人及你
    2021-02-01 13:49

    Apache Commons Lang 3 contais the class EnumUtils. If you aren't using Apache Commons in your projects, you're doing it wrong. You are reinventing the wheel!

    There's a dozen of cool methods that we could use without throws an Exception. For example:

    Gets the enum for the class, returning null if not found.

    This method differs from Enum.valueOf in that it does not throw an exceptionfor an invalid enum name and performs case insensitive matching of the name.

    EnumUtils.getEnumIgnoreCase(SeasonEnum.class, season);
    

提交回复
热议问题