Best way to define error codes/strings in Java?

前端 未结 13 791
别那么骄傲
别那么骄傲 2020-12-12 08:37

I am writing a web service in Java, and I am trying to figure out the best way to define error codes and their associated error strings. I need to have a nu

13条回答
  •  星月不相逢
    2020-12-12 09:10

    Please follow the below example:

    public enum ErrorCodes {
    NO_File("No file found. "),
    private ErrorCodes(String value) { 
        this.errordesc = value; 
        }
    private String errordesc = ""; 
    public String errordesc() {
        return errordesc;
    }
    public void setValue(String errordesc) {
        this.errordesc = errordesc;
    }
    

    };

    In your code call it like:

    fileResponse.setErrorCode(ErrorCodes.NO_FILE.errordesc());
    

提交回复
热议问题