Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 670839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:18:28+00:00 2026-05-14T00:18:28+00:00

We have a REST API where clients can supply parameters representing values defined on

  • 0

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 lookup method to each Enum. Seems like we’re just copying code (bad). Is there a better practice?

public enum MyEnum {
    A, B, C, D;

    public static MyEnum lookup(String id) {
        try {
            return MyEnum.valueOf(id);
        } catch (IllegalArgumentException e) {
            throw new RuntimeException("Invalid value for my enum blah blah: " + id);
        }
    }
}

Update: The default error message provided by valueOf(..) would be No enum const class a.b.c.MyEnum.BadValue. I would like to provide a more descriptive error from the API.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:18:29+00:00Added an answer on May 14, 2026 at 12:18 am

    Probably you can implement generic static lookup method.

    Like so

    public class LookupUtil {
       public static <E extends Enum<E>> E lookup(Class<E> e, String id) {   
          try {          
             E result = Enum.valueOf(e, id);
          } catch (IllegalArgumentException e) {
             // log error or something here
    
             throw new RuntimeException(
               "Invalid value for enum " + e.getSimpleName() + ": " + id);
          }
    
          return result;
       }
    }
    

    Then you can

    public enum MyEnum {
       static public MyEnum lookup(String id) {
           return LookupUtil.lookup(MyEnum.class, id);
       }
    }
    

    or call explicitly utility class lookup method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a REST API which clients routinely POST and PUT data to. When
I have a REST API written in PHP located on Apache server. It takes
I have successfully created a PHP REST API which resides on my server. I
I have a scenario in which I have REST API which manages a Resource
im trying to send rest api Users.getLoggedInUser i have all the secret session and
We are trying to implement a REST API for an application we have now.
I'm developing a REST api. To simplify my question, I have an API that
I have a REST server and a client application running on a mobile device.
I wonder what the REST API clients are available for using from Ruby (not
I've read about oAuth, Amazon REST API, HTTP Basic/Digest and so on but can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.