public class DefaultCountValidationInterceptor implements ValidateInterceptor
{
@Override
public void onValidate(final Object object, final InterceptorContext interceptorContext) throws InterceptorException
{
if (object instanceof BaseStoreModel)
{
final BaseStoreModel baseStoreModel = (BaseStoreModel) object;
if (baseStoreModel.getCount() < 0 || baseStoreModel.getCount() > 100)
{
throw new InterceptorException("Count should be between 0 and 100");
}
}
}
}
拦截器配置:
<bean id="defaultCountValidationInterceptor"
class="se.istone.hybris.maersk.core.interceptors.DefaultCountValidationInterceptor " />
<bean id="defaultCountValidationInterceptorMapping"
class="de.hybris.platform.servicelayer.interceptor.impl.InterceptorMapping">
<property name="interceptor"
ref="defaultCountValidationInterceptor" />
<property name="typeCode" value="BaseStore" />
</bean>
验证错误消息在 Hybris5.4 HMC 中正确显示,但是 它在 Hybris 6.7(1905) Backoffice 中不起作用
最佳答案
由于 ModelSavingExceptionTranslationHandler.toString()
中的 OOTB 代码,您总是会收到默认消息。
public class ModelSavingExceptionTranslationHandler extends ModelExceptionTranslationHandler {
public static final String I18N_UNEXPECTED_UPDATE_ERROR = "unexpected.update.error";
public boolean canHandle(Throwable exception) {
return exception instanceof ModelSavingException
|| exception instanceof ObjectSavingException && exception.getCause() instanceof ModelSavingException;
}
public String toString(Throwable exception) {
return this.getLabelByKey("unexpected.update.error");
}
}
当您抛出 InterceptorException
时,Hybris 在内部抛出 ModelSavingException
并将您的 InterceptorException
作为 Cause。
后台异常由 ExceptionTranslationService
处理,它包含处理不同类型异常的处理程序列表。对于 ModelSavingException
,使用了 ModelSavingExceptionTranslationHandler
。
由于 OOTB 处理程序直接显示默认消息,您可以覆盖此类,也可以创建自己的异常转换处理程序并将其添加到处理程序列表中。
关于validation - 验证未在 1905 Backoffice 中显示的拦截器错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62387784/