Grails 3.2中的Java 8 LocalDate JSON视图 [英] Java 8 LocalDate in Grails 3.2 JSON view
问题描述
当我尝试使用 json view ,我收到一个堆栈溢出异常,这似乎与GSON实现不知道Java 8 LocalDateTime有关。
下面是一个示例( grails视图和JSON文档):
域类Person
import java.time.LocalDate
class Person
{
Sting name
LocalDate birthDate
}
PersonController
class PersonController
{
def index(){
respon Person。 findAll()as List< Person>
$ / code $ / pre
$ p $ index $ / $ $ $
$ b @Field List< Person> personList
json tmpl.person(personList)
_person.gson模板文件
model
{
Person person
}
json g.parse(person)
当我试图打开这个视图时,抛出了一个堆栈溢出异常:
$ b
引起:grails.views.ViewRenderException:呈现视图时出错:
null
at grails.views.AbstractWritableScript .writeTo(AbstractWritableScript.groovy:43)
at grails.plugin.json.view.api.internal.DefaultGrailsJsonViewHelper $ 6.writeTo(DefaultGrailsJsonViewHelper.groovy:677)
当我将_person.gson简化为:
...
json
{
name person.name
}
Thew查看按预期工作。
我的问题是
是否有默认方式处理Java 8 LocalDate / Time PROPERT Grails JSON视图中是否存在?
如果不是,我该如何永久注册一个新的GSON构建器
- 这个问题是相关的:
-
我试图实现这一点: https://github.com/gkopff/gson-javatime-serialisers 但这不会将自己注入为新的默认de / serializer并需要手动使用
解决方案这些java 8日期支持尚未在json视图中实现。这可以在当时使用视图1.2.0(当它被发布时)以及最新版本的grails-java8插件。
编辑:到澄清,只有在使用内置渲染策略时才支持(g.render,hal.render,jsonapi.render等)。您仍然可以以任何您想要的方式自行构建视图:
json {
someLocalDate LocalDate.now.format( ...)
}
Edit2:Json视图1.2与grails一起发布-java8插件(也是1.2版)。 Java 8的日期类型渲染现在支持2个插件一起使用。
When I try to parse a simple domain model object containing a Java 8 LocalDateTime using a json view, I receive a stack overflow exception, that seems to be related to the fact that the GSON implentation does not know about Java 8 LocalDateTime.
Here is an example (documentation of grails views and JSON):
Domain class "Person"
import java.time.LocalDate
class Person
{
Sting name
LocalDate birthDate
}
PersonController
class PersonController
{
def index() {
respond Person.findAll() as List<Person>
}
}
index.gson view
@Field List<Person> personList
json tmpl.person(personList)
_person.gson template file
model
{
Person person
}
json g.parse(person)
When I try to open this view, a stack overflow exception is thrown:
Caused by: grails.views.ViewRenderException: Error rendering view:
null
at grails.views.AbstractWritableScript.writeTo(AbstractWritableScript.groovy:43)
at grails.plugin.json.view.api.internal.DefaultGrailsJsonViewHelper$6.writeTo(DefaultGrailsJsonViewHelper.groovy:677)
When I simplify the _person.gson to this:
...
json
{
name person.name
}
Thew view works as expected.
My question is
Is there a default way to handle Java 8 LocalDate/Time properties in Grails JSON views?
And If not, how can I register a new GSON builder permanently
- This question is related: How to serialize and deserialize Java 8's java.time types with Gson?
- I tried to implemented this: https://github.com/gkopff/gson-javatime-serialisers but this won't inject itself as new default de/serializer and needs to be used manually
解决方案 These java 8 date support is not yet implemented in json views. This will work out of the box with views 1.2.0 (when it is released) and the latest version of the grails-java8 plugin at that time.
Edit: To clarify, it is only not supported when using the built in rendering strategy (g.render, hal.render, jsonapi.render, etc). You can still construct the views yourself in any way you want:
json {
someLocalDate LocalDate.now.format(...)
}
Edit2: Json views 1.2 has been released along with the grails-java8 plugin (also version 1.2). Java 8 date type rendering is now supported out of the box with the 2 plugins together.
这篇关于Grails 3.2中的Java 8 LocalDate JSON视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文