有时用枚举代替常量
如结果集状态码为例,有状态码,状态信息等
@Getter
public enum ResultEnum {
SUCCESS(0, "success"),
FAIL(-1,"fail"),
ERROR_400(400, "400"),
ERROR_401(401, "401"),
ERROR_402(402, "402"),
ERROR_403(403, "403"),
ERROR_404(404, "404"),
ERROR_500(500, "500"),
private Integer code;
private String message;
private String customMessage;
/**
*
* @param code
* @param message
* @param customMessage 可指定信息 SUCCESS(0, "success", "成功-指定信息"), // 可以指定显示信息
*/
ResultEnum(Integer code, String message, String customMessage) {
this.code = code;
this.message = message;
this.customMessage = customMessage;
}
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public String getMessage() {
if (customMessage != null) {
return customMessage;
}
return MessageUtils.get(message);
}
/**
* 带替换参数
* @param args
* @return
*/
public String getMessage(Object[] args) {
if (customMessage != null) {
return customMessage;
}
return MessageUtils.get(message, args);
}
}