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>
你总是得到默认的消息,因为OOTB代码在这里。ModelSavingExceptionTranslationHandler.toString()
.
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
作为原因。
后台异常由 ExceptionTranslationService
其中包含处理程序列表,用于处理不同类型的异常。对于 ModelSavingException
, ModelSavingExceptionTranslationHandler
被使用。
由于OOTB处理程序是直接显示默认消息的,所以你可以覆盖这个类,或者你可以创建自己的异常转换处理程序并将其添加到处理程序列表中。
文档介绍 ->.Htps:/help.sap.comviewer5c9ea0c629214e42b727bf08800d8dfa1905en-US8bc9570b8669101101.html https:/help.sap.comviewer5c9ea0c629214e42b727bf08800d8dfa1905en-US8bc9570b86691014a901c290d2c5f107.html。
可能是复制你的代码和配置出错,但你的InterceptorMapping中的bean ref有错误,是大写的。
"DefaultCountValidationInterceptor"
而不是
"defaultCountValidationInterceptor"
除此之外,你应该把声明的bean id和类属性末尾的空格去掉。
我一直试图在1905 Hybris OOTB代码上重现你的错误,但我无法重现,我用AddressValidator(也实现了ValidatorInteceptor)试了一下,得到的信息是backoffice上的错误提醒中显示的异常。
问题可能是如果抛出了一个不同于InterceptorException的异常,比如nullPointerException,你确定让count属性被填满而不是null吗?
这可能是不可能的,但如果改变baseStore上的任何属性,然后保存,验证拦截器将被运行。我的建议是在访问count属性之前先检查nulls是否为空,像。
if (baseStoreModel.getCount() !=null && (baseStoreModel.getCount() < 0 || baseStoreModel.getCount() > 100))
这样做条件可以避免null问题,因为如果你的count属性被设置为null,只要第一个条件被评估为false(baseStoreModel.getCount() != null),条件就会退出。
另一种避免null错误的方法是在你的*-items.xml定义上有一个defaultValue,添加一个。
<defaultvalue>Integer.valueOf(0)</defaultvalue>
@JagadeeshKumar, 就像@Zaheer Attar在他的回答中说的那样。https:/stackoverflow.coma624158303346298。 (真的很有道理),你可以有自己的ModelExceptionTranslation Handler。但是,在定制任何东西之前,我会先检查一下,错误是什么,我的意思是,在
de.hybris.platform.platformbackoffice.service.handlers.ModelSavingExceptionTranslationHandler
在那里使用一个调试点来检查异常的内容,原因,甚至找出你的验证器的消息是否在那里。一旦你知道了异常,你就可以知道为什么OOTB TranslationHandler没有按照预期工作的真正原因。
开发一个新的处理程序不能解决错误的根本原因,这可能会在接下来的未来产生相邻的问题。