Grails 无法对具有JSON对象的类使用不可变注释
我有一个简单的对象,我想使它不可变Grails 无法对具有JSON对象的类使用不可变注释,grails,groovy,Grails,Groovy,我有一个简单的对象,我想使它不可变 import grails.converters.JSON @Immutable class Response { JSON jsonResponse int httpStatusCode String message } 当我尝试编译时,我得到: | Error Fatal error during compilation org.apache.tools.ant.BuildException: Compilation Faile
import grails.converters.JSON
@Immutable
class Response {
JSON jsonResponse
int httpStatusCode
String message
}
当我尝试编译时,我得到:
| Error Fatal error during compilation org.apache.tools.ant.BuildException: Compilation Failed
java.lang.NullPointerException
at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:163)
如果我这样做:
@Immutable(knownImmutableClasses = [JSON])
我也一样
如果我删除JSON对象,一切都很好。这就是问题所在。有什么想法吗
我正在使用Grails2.2.1和Groovy 2.1.6
谢谢我不太清楚您为什么会得到这个
NullPointerException
,但是将带有JSON
属性的类标记为不可变是没有任何意义的,因为JSON
实际上不是不可变的。您可以对编译器撒谎,并用@immutable(knownImmutableClasses=[JSON])
假装它是不可变的,但这实际上并不能使它不可变
import grails.converters.JSON
@Immutable
class Response {
JSON jsonResponse
int httpStatusCode
String message
}
如果你把你的班级改成
@Immutable
class Response {
String jsonResponse
int httpStatusCode
String message
}
并将其实例化如下:
def someObject = [1: 2, foo: 'foo']
def response = new Response(
jsonResponse: someObject as JSON,
httpStatusCode: 200,
message: 'hello'
)
那应该能解决你的问题