Code Junkie
Code Junkie

Reputation: 7788

Accessing service from within src/groovy

I've built some custom marshallers and I'm trying to access the messages service. I have the following code.

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
        }
    }
}

I'm getting the following exception

Cannot invoke method getMessage() on null object. Stacktrace follows: java.lang.NullPointerException: Cannot invoke method getMessage() on null object at org.hri.leaverequest.marshaller.ErrorsMarshaller$_register_closure1_closure2.doCall(ErrorsMarshaller.groovy:26) at org.hri.leaverequest.marshaller.ErrorsMarshaller$_register_closure1.doCall(ErrorsMarshaller.groovy:21) at grails.converters.JSON.value(JSON.java:202) at grails.converters.JSON.convertAnother(JSON.java:162) at 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) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:744)

Upvotes: 0

Views: 285

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

Depending on your version of Grails (2.x in this case) you may need to include the packages in "Spring bean scanning". Look in your Config.groovy and add them:

// packages to include in Spring bean scanning
grails.spring.bean.packages = ['com.example.my.package.here']

Upvotes: 0

Related Questions