Grails는 모든 오류를 처리하는 ValidationErrorsMarshaller
의 인스턴스를 등록하므로 FieldError 마샬 러가 절대 호출되지 않습니다. ConvertersGrailsPlugin
(플러그인의 설명) 당신이 당신의 요구를 (테스트하지 맞게 marshalObject()
을 무시하고 다른 빈을 생성하고 같은 이름으로 등록 할 수 있도록이, 스프링 빈으로 등록되어 보면
//convertAnother is not called for each error. That's the reason of your custom marshalled not been called.
for (Object o : errors.getAllErrors()) {
if (o instanceof FieldError) {
FieldError fe = (FieldError) o;
writer.object();
json.property("object", fe.getObjectName());
json.property("field", fe.getField());
json.property("rejected-value", fe.getRejectedValue());
Locale locale = LocaleContextHolder.getLocale();
if (applicationContext != null) {
json.property("message", applicationContext.getMessage(fe, locale));
}
else {
...
,해야 할 수도 있습니다 더 많은 코드).
class MyErrorsMarshaller implements ObjectMarshaller<JSON>, ApplicationContextAware {
ApplicationContext applicationContext;
public boolean supports(Object object) {
return object instanceof Errors;
}
public void marshalObject(Object object, JSON json) throws ConverterException {
...
}
}
resources.groovy ValidationErrorsMarshaller위한
jsonErrorsMarshaller(MyErrorsMarshaller)
errorsJsonMarshallerRegisterer(ObjectMarshallerRegisterer) {
marshaller = { MyErrorsMarshaller om -> }
converterClass = grails.converters.JSON
}
참조.
ConvertersGrailsPlugin에 대한 참조.
호기심있는 사람들을위한
우선 순위를 설정하려고 시도 했습니까? 'registerObjectMarshaller (java.lang.Class clazz, int priority, groovy.lang.Close callable)' – Bart