0

我创建了一个 RESTful 网络服务,这个网络服务使用 mysql 数据库。这是按照使用 Netbeans IDE 的操作指南完成的。

除了一件小事,一切都很好。

有一个表设置为“时间”类型(默认值 00:00:00),但由于某种原因,当我访问 wadl 时,我看到:

<time>1970-01-01T17:00:00+01:00</time>

我不是一个非常好的 Java 程序员,但我在网络服务的源代码中看到了 Netbeans 所做的:

public void setDate(Date time) {
    this.time = time;
}

我如何将其更改为时间值?有我可以使用的标准课程吗?

[编辑]

我正在运行一个 glassfish 服务器,我在其中部署了一个 Netbeans 生成的战争文件。

使用 Netbeans 和 mysql 生成 RESTful Web 服务的教程

(netbeans.org/kb/docs/websvc/rest.html#entities-and-services)

4

2 回答 2

0

In the database, the time value is usually just stored as a long value ignoring the date part, which results in that when it's converted to a date, the date value is the unix epoch value (i.e. 0).

So i'm not sure that this is an issue, just convert it back to a Date on the receiving end and you'll have a date with the time properly set.

EDIT: I suppose you have a transfer object of some sort where you define this "time" parameter? Or are you using hibernate or similar objects as the output to your rest xml generator?

If so, have you tried changing the data type from Date to Time?

Another way would be to change the type to String and in the setDate method use SimpleDateFormat to get a string on the exact form you want.

于 2011-09-20T07:50:20.603 回答
0

RESTful Web 服务公开的类型在类中定义YourTableFacadeREST。尝试修改该类中对应方法的返回类型。

已编辑问题是,当暴露的对象更复杂并且您的日期只是该对象的“一部分”时,上述想法将不起作用。可能最好的解决方案是使用读取对象的代码来处理转换。

如果您的目标只是显示这些数据(即对于 GET 类型的请求),您可以尝试使用视图。我从未在视图上完成 Jersey RESTful Web 服务,但它应该适用于 GET 端。您的视图应显示原始表,其中日期时间字段转换为日期。请参阅此处在 MySql 中创建视图的语法:http: //dev.mysql.com/doc/refman/5.0/en/create-view.html

于 2011-09-20T11:48:59.667 回答