简体   繁体   English

Google端点-java.sql.Timestamp-JSON

[英]Google Endpoint - java.sql.Timestamp - JSON

How to pass java.sql.Timestamp as JSON from front-end? 如何从前端将java.sql.Timestamp作为JSON传递? By default, I tried giving like 2015-09-29T12:30:00 in API explored. 默认情况下,我尝试在探索的API中给出类似2015-09-29T12:30:00的信息。 It is not working. 它不起作用。

Also, if anyone know, how to use @ApiTransformer for in-build java classes. 另外,如果有人知道,如何将@ApiTransformer用于内置Java类。 OR is there an option to use this annotation at property level instead of POJO class level. 或可以在属性级别而不是POJO类级别使用此注释。

Answer: 回答:

    Timestamp timestamp = new Timestamp(new Date().getTime());
    ObjectMapper mapper = new ObjectMapper();
    try {
        String jacksonDate = mapper.writeValueAsString(timestamp);
        System.out.println("JSON Timestamp: "+jacksonDate);
        System.out.println("JAVA Timestamp: "+mapper.readValue(jacksonDate, Timestamp.class));
    } catch (IOException e) {
        e.printStackTrace();
    }

output: 输出:
JSON Timestamp: 1443772585286 JSON时间戳:1443772585286
JAVA Timestamp: 2015-10-02 11:56:25.286 JAVA时间戳:2015-10-02 11:56:25.286

Even it accepts: yyyy-MM-DD'T'HH:mm:ss 甚至接受:yyyy-MM-DD'T'HH:mm:ss

I suggest you to think doing the opposite. 我建议您考虑相反。

Let's say you get values from database to java beans (POJO) and serialize them to JSON objects. 假设您从数据库获取值到Java Bean(POJO),然后将它们序列化为JSON对象。 Print them out especially Timestamp values. 打印出来,尤其是时间戳记值。

Then use the same format values as a short-cut answer. 然后,将相同的格式值用作快捷方式答案。

On the other hand, in Jackson, I know that java.sql.Date accepts "long" values of time measure. 另一方面,在Jackson中,我知道java.sql.Date接受时间量度的“长”值。 Timestamp may use the same approach. 时间戳可以使用相同的方法。 "time in milliseconds" is a long value though. 不过,“以毫秒为单位的时间”是一个长值。

On the other hand if you look for handling marshalling and unmarshalling of types in Rest systems you can check this out. 另一方面,如果您希望在Rest系统中处理类型的编组和解组,则可以进行检查。

http://www.developerscrappad.com/2308/java/java-ee/rest-jax-rs/java-rest-jax-rs-2-0-how-to-handle-date-time-and-timestamp-data-types/ http://www.developerscrappad.com/2308/java/java-ee/rest-jax-rs/java-rest-jax-rs-2-0-how-to-handle-date-time-and-timestamp-数据类型/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM