如何将从ResultSet获取的时间戳值转换为JSON对象?

时间:2016-10-23 12:42:34

标签: java json heroku jersey jax-rs

我正在使用Jersey(JAX-RS)&我有以下课 Post.java

public class Post {

    private String data;
    private Long time;

    public Post() {
        data = null;
        time = null;
    }

    public Post(final String data, final Long time) {
        this.data = data;
        this.time = time;
    }

    public String getData() {
        return data;
    }

    public Long getTime() {
        return time;
    }

    public void setData(final String data) {
        this.data = data;
    }

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

}

和班级 PostBundle.java

import java.util.List;

public class PostBundle {

    private String statusCode;
    private String message;
    private List<Post> posts;

    public PostBundle() {
        statusCode = null;
        message = null;
        posts = null;
    }

    public PostBundle(final String statusCode, final String message, final List<Post> posts) {
        this.statusCode = statusCode;
        this.message = message;
        this.posts = posts;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public String getMessage() {
        return message;
    }

    public List<Post> getPosts() {
        return posts;
    }

    public void setStatusCode(final String statusCode) {
        this.statusCode = statusCode;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public void setPosts(final List<Post> posts) {
        this.posts = posts;
    }

}

我在Heroku中有一个数据库,我正在尝试使用以下代码获取timestamp列的值: -

final String sqlCheckWallPost = 
                "SELECT user_post, post_time FROM wall_post WHERE user_id = ?;";

        preparedStatement = null;
        preparedStatement = connection.prepareStatement(sqlCheckWallPost);
        preparedStatement.setInt(1, id);

        resultSet = preparedStatement.executeQuery();

        if  (resultSet.next()) {
            posts.add(new Post(
                    resultSet.getString("user_post"),
                    Long.valueOf(resultSet.getTime("post_time").getTime())));
            while   (resultSet.next()) {
                posts.add(new Post(
                        resultSet.getString("user_post"), 
                        Long.valueOf(resultSet.getTime("post_time").getTime())));                       
            }
            postBundle = new PostBundle("200", "OK", posts);

上面的代码是接收POST请求并生成MediaType.APPLICATION_JSON

的API的一部分

但每当我向它发出POST请求时,它都会返回一个JSON数组,但它不会为 Post 对象的time部分返回任何内容。

我最终得到了这个:

{"message":"OK","posts":[{"data":"blah blah"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"someum la deh!!"},{"data":"WHADDUP DAWG"},{"data":"WHADDUP DAWG!!!"},{"data":"TEST"},{"data":"HEY THERE!!"},{"data":"HEY THERE!!"},{"data":"HEY THERE!!"},{"data":"HEY THERE 12!!"}],"statusCode":"200"}

如果您查看上面的响应,它不包含任何时间部分,它甚至没有提到密钥。这里出了什么问题?我知道以下内容始终会产生timestamp值:

posts.add(new Post(
                            resultSet.getString("user_post"),
                            Long.valueOf(resultSet.getTime("post_time").getTime())));

因为当我仅提取timestamp值并仅返回LongMediaType.TEXT_PLAIN时,它会显示以毫秒为单位的时间。但是,对于JSON,它没有。

我哪里错了?

0 个答案:

没有答案