I have created a Grails 4.0 application using the VUE profile and am using the JSON Views (http://views.grails.org/latest/#_json_views) and everything works correctly but I haven't found a way to use domain methods in the .gson template
An example that works perfectly fine:
Person.groovy domain class
class Person {
String firstName
String lastName
String fullName(){
return "$firstName $lastName"
}
}
PersonController
class PersonController {
def show(){
respond Person.get(params.id)
}
}
/views/person/_person.gson
model {
Person person
}
json {
lastName person.lastName
firstName person.firstName
//fullName person.fullName() -- this line doesn't compile
}
This is a basic example of what I'm trying to do but I can't get anything like this to compile and I haven't seen in the docs if it's even possible. I also tried calling the method in the domain class "getFullName()" and then in the gson file doing "fullName person.fullName" but that didn't work either.
Is there a way to use the methods of a domain class in the .gson file?
UPDATE: This is an example of the stacktrace log with the getFullName()
[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
And this is an example of it as fullName() method
[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
One of the error messages you show there includes the following:
[Static type checking] - No such property: fullName for class: Person
@ line 8, column 8.
fullName person.fullName
^
1 error
That looks like you are referring to person.fullName
instead of person.fullName()
. person.fullName
would work if you had a method in the Person
class named getFullName()
, but you don't.
See the project at https://github.com/jeffbrown/fullnamequestion.
import fullnamequestion.Person
model {
Person person
}
json {
lastName person.lastName
firstName person.firstName
fullName person.fullName()
}
That works fine:
~ $ 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
Send a request to render the view:
~ $ curl http://localhost:8080/person/1
{"lastName":"Lee","firstName":"Geddy","fullName":"Geddy Lee"}