简体   繁体   English

Grails 2.5.5在具有equals方法的域类上使用'as JSON'时出错

[英]Grails 2.5.5 error with 'as JSON' on a domain class that has an equals method

I just came into an error when I tried to get the current logged user from springSecurityService and render it as JSON. 当我尝试从springSecurityService获取当前登录用户并将其呈现为JSON时,我刚刚遇到错误。

def user = springSecurityService.getCurrentUser()
println user as JSON // EXCEPTION!

User is a domain class and had an equals method, the error below happens because the 'as JSON' seems to be calling the equals method (I don't understand why). 用户是一个域类并且有一个equals方法,下面的错误发生是因为'as JSON'似乎在调用equals方法(我不明白为什么)。

User looks like this: 用户看起来像这样:

class User implements Serializable {
   ...
   String username
   String password
   String email
   ...

   @Override
   boolean equals(other)
   {
      is(other) || (other.instanceOf(User) && other.username == username)
   }
   ...
}

This is the exception: (the error is on the .instanceOf(User)) 这是例外:(错误发生在.instanceOf(User)上)

2016-10-11 05:40:24,706 [http-bio-8090-exec-1] ERROR errors.GrailsExceptionResolver  - MissingMethodException occurred when processing request: [POST] /ehr/query/save

No signature of method: org.codehaus.groovy.grails.web.json.JSONObject$Null.instanceOf() is applicable for argument types: (java.lang.Class) values: [class com.cabolabs.security.User]. Stacktrace follows:

Message: No signature of method: org.codehaus.groovy.grails.web.json.JSONObject$Null.instanceOf() is applicable for argument types: (java.lang.Class) values: [class com.cabolabs.security.User]

    Line | Method
->>   45 | equals    in com.cabolabs.security.User
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    176 | value     in grails.converters.JSON
|    134 | render .  in     ''
|    250 | save      in com.cabolabs.ehrserver.query.QueryController
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|     53 | doFilter  in grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter
|     62 | doFilter  in grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run . . . in java.lang.Thread

Is anyone experimenting the same? 有人试验过吗? Is this the expected behavior or a bug? 这是预期的行为还是错误?

Try not to give equals() implementation by yourself, and use this, @EqualsAndHashCode , annotation, provided by Groovy instead. 尽量不要自己给equals()实现,并使用Groovy提供的@EqualsAndHashCode注释。 See if that works. 看看是否有效。

Or 要么

Check the instanceOf() availability before hand, like this 事先检查instanceOf()可用性,就像这样

(obj.respondsTo('instanceOf') && obj.instanceOf(User))

using respondsTo() method. 使用respondsTo()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM