grails - 在Grails JSON View 中使用域方法

标签 grails json-view

我已经使用VUE配置文件创建了Grails 4.0应用程序,并且正在使用JSON View (http://views.grails.org/latest/#_json_views),并且一切正常,但是我还没有找到在.gson模板中使用域方法的方法。

一个很好的例子:

Person.groovy域类

class Person {

    String firstName
    String lastName

    String fullName(){
        return "$firstName $lastName"
    }
}

人员 Controller
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
}

这是我正在尝试做的一个基本示例,但是我无法编译这样的东西,即使有可能,我也没有在文档中看到。我也尝试过在域类“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

这是fullName()方法的一个示例
[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.fullName类中有一个名为Person的方法,则getFullName()将起作用,但您没有。

请参阅https://github.com/jeffbrown/fullnamequestion上的项目。

https://github.com/jeffbrown/fullnamequestion/blob/81cb45f176f887edf90de783a976c48154c3f9bc/server/grails-app/views/person/_person.gson
import fullnamequestion.Person

model {
    Person person
}

json {
    lastName person.lastName
    firstName person.firstName
    fullName 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

发送请求以渲染 View :
~ $ curl http://localhost:8080/person/1
{"lastName":"Lee","firstName":"Geddy","fullName":"Geddy Lee"}

关于grails - 在Grails JSON View 中使用域方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581635/

相关文章:

grails - Grails表单重定向/渲染的最佳实践

grails - Grails在g:each中检索 Controller 参数

grails - 如何使用createCriteria比较收集项目?

java - @JsonView 未传播到 ("nested")自定义序列化器

grails - 如何创建显示JSON View 的grails 3插件?

json - 在JSON View 中,如何将对象展平为单个字符串?

tomcat - CloudFoundry 上的 Grails 应用程序不会重新启动

firefox-developer-tools - 如何在 Firefox Developer Edition 中禁用新的 JSON 查看器/阅读器?

java - @JsonView 不过滤属性(Spring 4.1.0.RC2,Jackson 2.3.2)

grails - 无法找到 java 编译器 com.sun.tools.javac.Main,请更改您的类加载器设置