RESTful Webservice时间值问题

发布于 12-05 14:22 字数 605 浏览 2 评论 0原文

我创建了一个 RESTful Web 服务,该 Web 服务使用 mysql 数据库。这是使用 Netbeans IDE 按照说明完成的。

除了一件小事之外,一切都运转良好。

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

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

我不是一个很好的 Java 程序员,但我在源代码中看到Netbeans 制作的 Web 服务的说明:

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

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

[编辑]

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

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

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

I have created a RESTful webservice and this webservice uses a mysql database. This was done following a howto using the Netbeans IDE.

All is working fine except for one little thing.

There is one table that is set as a 'time' type (default values 00:00:00) but for some reason when i access the wadl i get to see:

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

I am not a very good Java programmer but i saw in the source of the webservice that Netbeans made this:

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

How do i change this to just the time value? Are there standard classes that i can use?

[edit]

I am running a glassfish server where i deployed a Netbeans generated war file.

The tutorial to generate a RESTful webservice using Netbeans and mysql

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

安人多梦2024-12-12 14:22:36

在数据库中,时间值通常只是存储为long值而忽略日期部分,这导致当它转换为日期时,日期值是unix纪元值(即0)。

所以我不确定这是一个问题,只需将其转换回接收端的日期,您就会得到一个时间设置正确的日期。

编辑:
我想你有一个某种类型的传输对象,你在其中定义了这个“时间”参数?或者您是否使用 hibernate 或类似的对象作为其余 xml 生成器的输出?

如果是这样,您是否尝试过将数据类型从“日期”更改为“时间”?

另一种方法是将类型更改为 String ,并在 setDate 方法中使用 SimpleDateFormat 来获取您想要的确切形式的字符串。

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.

中性美2024-12-12 14:22:36

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

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

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

The types exposed by the RESTful web service are defined in the class YourTableFacadeREST. Try to modify the returned type of the corresponding method in that class.

EDITED
The problem is that the above idea will not work when the exposed object is more complex and your date is only "a part" of that object. Probably the best solution is to handle the conversion with the code reading the object.

If your goal is just to display these data (i.e. for requests of type GET) you may try with a view. I have never done Jersey RESTful web services on a view, but it should work for the GET side. Your View should display the original table with the datetime field converted to date. See here the syntax for creating a view in MySql:
http://dev.mysql.com/doc/refman/5.0/en/create-view.html

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