弹簧袋鼠REST JSON控制器裂伤日期字段 [英] Spring-roo REST JSON controllers mangle date fields

查看:113
本文介绍了弹簧袋鼠REST JSON控制器裂伤日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我以两种方式使用数据的实体,我填充一个表,它的一些数据的页面加载时,当您单击的行列,我AJAX了该项目的详细信息,并显示出来在表单字段。我使用Spring的Roo的产生在服务器端REST端点和Backbone.js的客户端。

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.

在表负荷,日期字段是我所期望的格式,未来直出我的MySQL数据库(YYYY-MM-DD)的。当我得到我的AJAX的数据,日期字段来我作为Unix的时间值(如13236.66亿)。

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").

我可以转换在客户端,但是这是愚蠢的。任何想法,我怎么才能让我的JSON控制器不这样做呢?

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?

我已经试过推进这些领域的进入我的.java文件,并与@DateTimeFormat注释搞乱,但我看不出有什么差别。

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.

推荐答案

您可以转换日期,你想为你的JSON响应的任何格式。

You can transform the date to any format you want for your JSON response.

在你的情况,你已经使用默认的JSON日期变压器全部时间 java.util.Date 类型的字段。这基本上是给你的Spring Roo的生成的内容。看一看,并在你的* _Roo_Json方面,你会发现不便。像这样的:

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:

public java.lang.String PizzaOrder.toJson() {
    return new JSONSerializer().exclude("*.class").serialize(this);
}

这样的实现使用 flexjson.transformer.BasicDateTransformer 类转换日期为您服务。它是这样实现的:

Such an implementation uses the flexjson.transformer.BasicDateTransformer class to transform the date for you. It is implemented like this:

public class BasicDateTransformer extends AbstractTransformer {
    public void transform(Object object) {
        getContext().write(String.valueOf(((Date) object).getTime()));
    }
}

你需要的是用不同的,更强大的变压器。幸运的是,它配备了您的袋鼠,它被称为 flexjson.transformer.DateTransformer 。现在,为了正确地格式化日期,只是更换新的变压器,例如默认像这样的:

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:

public java.lang.String PizzaOrder.toJson() {
    return new JSONSerializer().exclude("*.class")
             .transform(new DateTransformer("MM/dd/yyyy"), Date.class)
             .serialize(this);
}

这就是: - )

That's all :-)

要知道,你也可能适用不同的日期(而不是只)像这样不同领域的转换:

Know that you may also apply different Date (and not only) transformations for different fields like this:

transform(new DateTransformer("MM/dd/yyyy"), "person.birthday")

有关的flexjson更多信息一起来看看 FLEXJSON项目页面

For more info about the flexjson take a look at FLEXJSON project page.

这篇关于弹簧袋鼠REST JSON控制器裂伤日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆