我已经构建了一些自定义marshallers,我正在尝试访问消息服务。我有以下代码。
resources.groovy
customObjectMarshallers(CustomObjectMarshallers){
marshallers = [
new PersonMarshaller(),
new DepartmentMarshaller(),
new ErrorsMarshaller()
]
}
CustomObjectMarshallers
class CustomObjectMarshallers {
List marshallers = []
def register(){
marshallers.each {
it.register()
}
}
}
ErrorsMarshaller
class ErrorsMarshaller {
@Autowired
def MessageSource messageSource
void register(){
JSON.registerObjectMarshaller(FieldError){ FieldError fieldError ->
def pMap = [:]
fieldError.properties.each { prop, val ->
println 'prop ' + prop
if( prop == 'rejected_value' || prop == 'arguments' || prop == 'class' || prop == 'codes' || prop == 'code' || prop == 'bindingFailure') {
return
} else if(prop == 'defaultMessage') {
pMap[prop] = messageSource.getMessage(val, fieldError, Locale.US)
} else {
pMap[prop] = MarshallerUtils.cleanPropertyValue(val)
}
}
return pMap
}
}
}
我收到以下异常
无法在null对象上调用方法getMessage()。 Stacktrace如下: java.lang.NullPointerException:无法在null对象上调用方法getMessage() at org.hri.leaverequest.marshaller.ErrorsMarshaller $ _register_closure1_closure2.doCall(ErrorsMarshaller.groovy:26) 在org.hri.leaverequest.marshaller.ErrorsMarshaller $ _register_closure1.doCall(ErrorsMarshaller.groovy:21) 在grails.converters.JSON.value(JSON.java:202) at grails.converters.JSON.convertAnother(JSON.java:162) 在grails.converters.JSON.value(JSON.java:202) at grails.converters.JSON.render(JSON.java:134) at org.hri.leaverequest.CalendarController.processRequest(CalendarController.groovy:154) at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:198) at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.java:53) at grails.plugin.springsecurity.web.authentication.RequestHolderAuthenticationFilter.doFilter(RequestHolderAuthenticationFilter.java:49) at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.java:82) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:744)
答案 0 :(得分:0)
根据您的Grails版本(在这种情况下为2.x),您可能需要在" Spring bean扫描"中包含这些包。查看Config.groovy
并添加它们:
// packages to include in Spring bean scanning
grails.spring.bean.packages = ['com.example.my.package.here']