Spring-roo REST JSON 控制器损坏日期字段
我有一个以两种方式使用的数据实体,我在页面加载时用其中的一些数据填充表格,当您单击该列的一行时,我通过 AJAX 获取该项目的详细信息并将其显示在表单字段中。我在服务器端使用 Spring-Roo 生成的 REST 端点,在客户端使用 Backbone.js。
当表加载时,日期字段具有我期望的格式,直接来自我的 MySQL 数据库(“yyyy-MM-dd”)。当我获取 AJAX 数据时,日期字段以 Unix 时间值的形式出现(例如“1323666000000”)。
我可以在客户端进行转换,但这很愚蠢。知道如何让我的 json 控制器不这样做吗?
我尝试将这些字段推入我的 .java 文件并使用 @DateTimeFormat 注释,但我看不出这有什么区别。
I have a data entity that I use in two ways, I populate a table with some of its data when the page loads, and when you click a row of that column, I AJAX up the details of that item and display them in form fields. I'm using Spring-Roo generated REST endpoints on the server side, and Backbone.js on the client.
When the table loads, date fields have the format I expect, coming straight out of my MySQL database ("yyyy-MM-dd"). When I get my AJAX data, date fields come to me as Unix time values (e.g. "1323666000000").
I can convert that on the client side, but that's stupid. Any idea how I can get my json controller to not do this?
I've tried pushing those fields into my .java file and messing with the @DateTimeFormat annotation, but I can't see that makes any difference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将日期转换为 JSON 响应所需的任何格式。
就您而言,您一直在
java.util.Date
类型字段中使用默认的 JSON 日期转换器。这基本上就是 Spring Roo 为您生成的内容。看看你的 *_Roo_Json 方面,你会发现一些东西。像这样:这样的实现使用
flexjson.transformer.BasicDateTransformer
类来为您转换日期。它是这样实现的:您想要的是使用不同的、更强大的变压器。幸运的是,您的 Roo 附带了它,名为
flexjson.transformer.DateTransformer
。现在,为了正确格式化您的日期,只需用新的转换器替换默认值,如下所示:仅此而已:-)
知道您还可以为不同的日期应用不同的
Date
(而不仅仅是)转换像这样的字段:有关 flexjson 的更多信息,请查看 FLEXJSON 项目页面。
You can transform the date to any format you want for your JSON response.
In your case, you've been using the default JSON date transformer all the time for the
java.util.Date
type fields. This is basically what gets generated for you by the Spring Roo. Take a look and in your *_Roo_Json aspects and you will find smth. like this:Such an implementation uses the
flexjson.transformer.BasicDateTransformer
class to transform the date for you. It is implemented like this:What you want is to use a different, more powerfull transformer. Luckily it comes with your Roo and it's called
flexjson.transformer.DateTransformer
. Now, in order to format your dates properly, just replace the default with the new transformer e.g. like this:That's all :-)
Know that you may also apply different
Date
(and not only) transformations for different fields like this:For more info about the flexjson take a look at FLEXJSON project page.