我有一個在Web服務實現中定義爲枚舉的帳戶類型列表。但是,當消費者調用Web服務時,它會傳遞一個需要轉換爲枚舉的字符串。驗證成功的字符串在Java中枚舉轉換
什麼是驗證給定的字符串將被成功轉換爲枚舉的好方法?
我正在使用以下方法,但這可能是濫用異常(根據Effective Java,第57項)。
AccountType accountType = null;
try{
accountType = AccountType.valueOf(accountTypeString);
}catch(IllegalArgumentException e){
// report error
}
if (accountType != null){
// do stuff
}else{
// exit
}
看到這個線程,它有這個http://stackoverflow.com/questions/604424/java-convert-string-to-enum –