我有一项服务,负责在注册页面成功填写后创建相关的帐户层次结构:
def userRole = Role.findByAuthority("ROLE_USER")
final account = new Account(email: command.email, password: command.password)
account.save(flush: true)
final publisher = new Publisher(name: command.name) //, account: account)
publisher.save(flush: true)
final accountRole = AccountRole.create account, userRole
无论我做什么,AccountRole.create account, userRole
都失败了:
static AccountRole create(Account account, Role role) {
def instance = new AccountRole(account: account, role: role)
instance.save() // throws NullPointerException
instance
}
我尝试了new AccountRole(account: account, role: userRole)
,但是当我手动调用save
时,这也失败了。 Account
和AccountRole
是由Grails 3 Spring Security插件生成(并略微调整)的类。
以下是失败的AccountRole.create(...)
调用的堆栈跟踪:
java.lang.reflect.InvocationTargetException: null
at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
at grails.plugin.springsecurity.web.filter.DebugFilter.invokeWithWrappedRequest(DebugFilter.groovy:102)
at grails.plugin.springsecurity.web.filter.DebugFilter.doFilter(DebugFilter.groovy:69)
at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: null
at package.AccountRole.create(AccountRole.groovy:48)
at package.RegistrationService.$tt__handleNewRegistration(RegistrationService.groovy:25)
版本信息:
| Grails Version: 3.1.5
| Groovy Version: 2.4.6
| JVM Version: 1.8.0_73
请救我脱离苦难!我现在花了好几个小时:(
答案 0 :(得分:2)
确实没有足够的信息,但instance
因为您刚刚使用构造函数调用创建它而不可能为null,但您应该检查确定,例如
static AccountRole create(Account account, Role role) {
def instance = new AccountRole(account: account, role: role)
println "instance null? ${instance == null}"
instance.save() // throws NullPointerException
instance
}
验证过程中出现问题的可能性更大,因此我的猜测是userRole
或account
出现了问题。检查userRole
是否已正确检索,并且帐户save()
调用中没有验证错误:
def account = new Account(email: command.email, password: command.password)
account.save(flush: true)
if (account.hasErrors()) {
log.warn "Failed to save Account $account: $account.errors"
}