初めてこのシナリオを処理するので、どのアプローチが最良の方法であるかを尋ねます。同じエラーメッセージ同じ例外について異なる通関メッセージを与える方法
@Override
public boolean saveParam(ParamDto dto) throws ParamException
{
try
{
return super.save(dto);
}
catch(DataIntegrityViolationException e)
{
throw new ParamException(ParamException.INTERNAL_SERVER_ERROR, messageSource.getMessage(CodeEnum.DUPLICATE_APP.getValue(), new Object[] { dto.getParamKey() }, Locale.ENGLISH));
}
catch (GenericException ge)
{
throw new ParamException(ge, ge.getRootCauseMessage());
}
}
を送信するすべてのDataIntegrityViolationException
ためここで
は、どのように我々は、同じ例外の異なる習慣メッセージを与えることができます。メッセージ文字列を確認する必要はありますか?
DataIntegrityViolationException
の下に2種類の例外があります。あなたはDataIntegrityViolationException
例外が例外の実際の原因を取得するためにgetMostSpecificCauseメソッドを使用してキャッチした後、あなたがすでにやっているとして
"ERROR: value too long for type character(1)" (id=172) ""
"ERROR: duplicate key value violates unique constraint "uk_param_key"\n Detail: Key (param_key)=(Test1) already exists."
JdbcExceptionTranslatorを使用できます。 http://stackoverflow.com/questions/2109476/how-to-handle-dataintegrityviolationexception-in-spring – Rehman