Grails JSON视图中域方法的使用
我已经使用VUE配置文件创建了一个Grails4.0应用程序,并且正在使用JSON视图,一切都正常工作,但是我还没有找到在.gson模板中使用域方法的方法 一个非常好的例子: Person.groovy域类Grails JSON视图中域方法的使用,grails,json-view,Grails,Json View,我已经使用VUE配置文件创建了一个Grails4.0应用程序,并且正在使用JSON视图,一切都正常工作,但是我还没有找到在.gson模板中使用域方法的方法 一个非常好的例子: Person.groovy域类 class Person { String firstName String lastName String fullName(){ return "$firstName $lastName" } } 个人控制器 class Person
class Person {
String firstName
String lastName
String fullName(){
return "$firstName $lastName"
}
}
个人控制器
class PersonController {
def show(){
respond Person.get(params.id)
}
}
/视图/个人/_person.gson
model {
Person person
}
json {
lastName person.lastName
firstName person.firstName
//fullName person.fullName() -- this line doesn't compile
}
这是我试图做的一个基本示例,但我无法编译类似的内容,而且我还没有在文档中看到是否可能。我还尝试在域类getFullName中调用该方法,然后在gson文件中调用fullName person.fullName,但也没有成功
是否有方法使用.gson文件中域类的方法
更新:
这是一个带有getFullName的stacktrace日志示例
[Static type checking] - No such property: fullName for class: Person
@ line 8, column 8.
fullName person.fullName
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:311)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1102)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:645)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:623)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:600)
at grails.views.ResolvableGroovyTemplateEngine$_createTemplate_closure2.doCall(ResolvableGroovyTemplateEngine.groovy:430)
... 71 common frames omitted
这是一个全名方法的例子
[Static type checking] - Cannot find matching method Person#fullName(). Please check if the declared type is correct and if the method exists.
@ line 8, column 8.
fullName person.fullName()
^
1 error
显示的其中一条错误消息包括以下内容:
[Static type checking] - No such property: fullName for class: Person
@ line 8, column 8.
fullName person.fullName
^
1 error
看起来您指的是person.fullName,而不是person.fullName。如果person类中有一个名为getFullName的方法,person.fullName将起作用,但实际上没有
请参阅上的项目
这很好:
~ $ git clone https://github.com/jeffbrown/fullnamequestion.git
Cloning into 'fullnamequestion'...
remote: Enumerating objects: 144, done.
remote: Counting objects: 100% (144/144), done.
remote: Compressing objects: 100% (120/120), done.
remote: Total 144 (delta 5), reused 144 (delta 5), pack-reused 0
Receiving objects: 100% (144/144), 188.53 KiB | 2.62 MiB/s, done.
Resolving deltas: 100% (5/5), done.
~ $
~ $ cd fullnamequestion/
~ $ ./gradlew server:bootRun
> Task :server:bootRun
Grails application running at http://localhost:8080 in environment: development
<==========---> 83% EXECUTING [18s]
> :server:bootRun
什么不是编译?是否存在要共享的错误或堆栈跟踪?@cfrick问题已更新为日志文件中的错误您是否尝试使用瞬态?看起来正是您要查找的:@Joshuamore transients与编译错误无关。如果他在自己的域类中有一个名为getFullName的方法,那么它们将是相关的,但这不是他/她所拥有的。我要做的是使用getFullName方法,使fullName暂时化,然后在视图中引用person.fullName,但与他/她在执行时相比,这是一个更好的想法,但这无助于解决问题所涉及的编译错误。@JeffScottBrown这就是我的建议。是一个使用瞬态的更干净的实现。然而,你是对的。我没有回答关于编译错误的问题。这是正确的,查看您的示例项目会有所帮助。我发现的一个问题是,如果在我的域类中,如果我调用我的方法def myMethod,它会给我一个NoSuchMethodError,但是如果我调用它String myMethod,它会正常工作。我原来是在做前一件事,这对我来说毫无意义,我无法复制它。方法的返回类型不应该是相关的。您在那里的评论与本问题中其他部分的一些项目相结合,向我表明您可能对属性与方法感到困惑。per.fullName将计算为fullName属性的值,这与调用per.fullName将调用名为fullName的方法不同。-我将您的项目更改为domain/Person.groovy,以说明我的意思如果您在应用程序运行时更改域类中的方法签名,这将解释您看到的行为。问题不是不能从JSON视图中调用域类上的方法。该问题与类重新加载问题有关。
~ $ git clone https://github.com/jeffbrown/fullnamequestion.git
Cloning into 'fullnamequestion'...
remote: Enumerating objects: 144, done.
remote: Counting objects: 100% (144/144), done.
remote: Compressing objects: 100% (120/120), done.
remote: Total 144 (delta 5), reused 144 (delta 5), pack-reused 0
Receiving objects: 100% (144/144), 188.53 KiB | 2.62 MiB/s, done.
Resolving deltas: 100% (5/5), done.
~ $
~ $ cd fullnamequestion/
~ $ ./gradlew server:bootRun
> Task :server:bootRun
Grails application running at http://localhost:8080 in environment: development
<==========---> 83% EXECUTING [18s]
> :server:bootRun
~ $ curl http://localhost:8080/person/1
{"lastName":"Lee","firstName":"Geddy","fullName":"Geddy Lee"}