我有一個特定的場景,我只能手動檢查違規情況,稍後在流程中進行檢查。Bean驗證:如何手動創建ConstraintViolation?
我想要做的就是扔了ConstraintViolationException
,並提供一個「真正的」 ConstraintViolation object
它(當我捕捉到了異常的堆棧,我用的是#{validatedValue}
和violation.getPropertyPath()
參數)。
我怎樣才能創建一個ConstraintViolation
我自己沒有框架通過註釋爲我做(我使用Hibernate驗證器)?
代碼示例:
List<String> columnsListForSorting = new ArrayList<String>(service.getColumnsList(domain));
Collections.sort(columnsListForSorting);
String firstFieldToSortBy = this.getTranslatedFieldName(domain.getClass().getCanonicalName(), sortingInfo.getSortedColumn());
if (!columnsListForSorting.contains(firstFieldToSortBy)){
throw new ConstraintViolationException(<what here?...>);
}
感謝。
到目前爲止,您的測試代碼是什麼? – Morfic
編輯你的問題,添加片段並使用「代碼」按鈕進行格式化,這使得它更容易閱讀:) – Morfic
謝謝,原來的問題已更新。 – odedia