Spring-roo REST JSON 控制器损坏日期字段

发布于 2024-12-18 08:57:46 字数 363 浏览 7 评论 0原文

我有一个以两种方式使用的数据实体,我在页面加载时用其中的一些数据填充表格,当您单击该列的一行时,我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-12-25 08:57:46

您可以将日期转换为 JSON 响应所需的任何格式。

就您而言,您一直在 java.util.Date 类型字段中使用默认的 JSON 日期转换器。这基本上就是 Spring Roo 为您生成的内容。看看你的 *_Roo_Json 方面,你会发现一些东西。像这样:

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

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

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

您想要的是使用不同的、更强大的变压器。幸运的是,您的 Roo 附带了它,名为 flexjson.transformer.DateTransformer。现在,为了正确格式化您的日期,只需用新的转换器替换默认值,如下所示:

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

仅此而已:-)

知道您还可以为不同的日期应用不同的Date(而不仅仅是)转换像这样的字段:

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

有关 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:

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

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()));
    }
}

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文