Java 为什么RESTAPI在嵌套对象上返回空JSON?

Java 为什么RESTAPI在嵌套对象上返回空JSON?,java,json,rest,heroku,Java,Json,Rest,Heroku,我正在尝试从Heroku应用程序数据库中检索给定电子邮件id的所有帖子 我有一个查询类,它处理所有请求(目前只处理GET) 下面是Query.java import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.auro.assignment.wallpostapi.model.PostBundle; @P

我正在尝试从Heroku应用程序数据库中检索给定电子邮件id的所有帖子

我有一个查询类,它处理所有请求(目前只处理
GET

下面是Query.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.auro.assignment.wallpostapi.model.PostBundle;

@Path("query")
public class Query {

    public Query() {

    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public PostBundle getPostBundle() {

        PostManager postManager = new PostManager();
        return postManager.getPostBundle("xyz.123@test.in");

    }

}
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.net.URISyntaxException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import com.auro.assignment.wallpostapi.dbconnection.DBConnectionManager;
    import com.auro.assignment.wallpostapi.model.*;

    public class PostManager {

        private PostBundle postBundle;
        private List<Post> posts;

        public PostManager() {
            postBundle = null;
            posts = new ArrayList<>();
        }

        public PostBundle getPostBundle(final String email) {

            try {
                Connection connection = DBConnectionManager.getConnection();
                final String query = "SELECT user_post, post_time FROM wall_post WHERE user_id = " +
                                                        "(SELECT user_id FROM user_info WHERE user_email = ?);";

                PreparedStatement preparedStatement = connection.prepareStatement(query);
                preparedStatement.setString(1, email);
                ResultSet resultSet = preparedStatement.executeQuery();

                if  (resultSet != null && resultSet.next()) {
                    while (resultSet.next()) {
                        posts.add(new Post(resultSet.getString("user_post"),resultSet.getString("post_time")));
                    }
                    postBundle = new PostBundle("200","SUCCESS!",posts);
                    return postBundle;
                }
                else {
                    postBundle = new PostBundle("404","NOT FOUND",null);
                    return postBundle;
                }           
            } catch (ClassNotFoundException | URISyntaxException | SQLException e) {
                StringWriter stringWriter = new StringWriter();
                PrintWriter printWriter = new PrintWriter(stringWriter);
                e.printStackTrace(printWriter);
                String stackTrace = stringWriter.toString();
                postBundle = new PostBundle("500",stackTrace,null);
                return postBundle;
            }

        }
    }

Following is the **PostBundle.java**

import java.util.List;

public class PostBundle {

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

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

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

    public String getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

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

}
public class Post {

    private String data;
    private String date;

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

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

    public String getData() {
        return data;
    }

    public String getDate() {
        return date;
    }

}
然后我有一个名为PostManager的东西,它将所有帖子与状态码和消息捆绑在一起

以下是PostManager.java的

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.auro.assignment.wallpostapi.model.PostBundle;

@Path("query")
public class Query {

    public Query() {

    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public PostBundle getPostBundle() {

        PostManager postManager = new PostManager();
        return postManager.getPostBundle("xyz.123@test.in");

    }

}
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.net.URISyntaxException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import com.auro.assignment.wallpostapi.dbconnection.DBConnectionManager;
    import com.auro.assignment.wallpostapi.model.*;

    public class PostManager {

        private PostBundle postBundle;
        private List<Post> posts;

        public PostManager() {
            postBundle = null;
            posts = new ArrayList<>();
        }

        public PostBundle getPostBundle(final String email) {

            try {
                Connection connection = DBConnectionManager.getConnection();
                final String query = "SELECT user_post, post_time FROM wall_post WHERE user_id = " +
                                                        "(SELECT user_id FROM user_info WHERE user_email = ?);";

                PreparedStatement preparedStatement = connection.prepareStatement(query);
                preparedStatement.setString(1, email);
                ResultSet resultSet = preparedStatement.executeQuery();

                if  (resultSet != null && resultSet.next()) {
                    while (resultSet.next()) {
                        posts.add(new Post(resultSet.getString("user_post"),resultSet.getString("post_time")));
                    }
                    postBundle = new PostBundle("200","SUCCESS!",posts);
                    return postBundle;
                }
                else {
                    postBundle = new PostBundle("404","NOT FOUND",null);
                    return postBundle;
                }           
            } catch (ClassNotFoundException | URISyntaxException | SQLException e) {
                StringWriter stringWriter = new StringWriter();
                PrintWriter printWriter = new PrintWriter(stringWriter);
                e.printStackTrace(printWriter);
                String stackTrace = stringWriter.toString();
                postBundle = new PostBundle("500",stackTrace,null);
                return postBundle;
            }

        }
    }

Following is the **PostBundle.java**

import java.util.List;

public class PostBundle {

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

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

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

    public String getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

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

}
public class Post {

    private String data;
    private String date;

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

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

    public String getData() {
        return data;
    }

    public String getDate() {
        return date;
    }

}
它在返回一个空的JSON


我的架构设计有什么问题吗?我猜可能发生了某种内部错误,但在PostManager方法中,我确保任何错误stacktrace也被捆绑,但我得到的是一个完全空白的屏幕。

我在这里看到一些错误:

首先,调用
resultSet.next()
两次

// Initially the cursor is positioned before the first row.
ResultSet resultSet = preparedStatement.executeQuery();

if  (resultSet != null && resultSet.next()) { // The cursor is moved to the first row
    while (resultSet.next()) { // The cursor is moved to the second row, so you skipped the
                               // first result and it only iterates from second to last one
        ...
    }
    postBundle = new PostBundle("200","SUCCESS!",posts); // posts only contains from
                                                         //second to last result. In case
                                                         //you only have one result,
                                                         //posts is an empty ArrayList

    return postBundle;
}
“xyz”有多少个帖子。123@test.in“你有吗?如果只有一个,那就是问题所在

Second,
preparedStatement.executeQuery()
从不返回
null

因此,您不需要检查它是否为
null

第三,如果电子邮件中没有任何帖子,则永远不要返回404,而应返回200,其中包含空json。

200确定-请求已成功。返回的信息与 响应取决于请求中使用的方法

404未找到-服务器未找到任何与 请求URI

404错误保留用于请求URI匹配

最后,您应该相应地修改代码:

您应该修改以下代码:

...
try {
    Connection connection = DBConnectionManager.getConnection();
    final String query = "SELECT user_post, post_time FROM wall_post WHERE user_id = " +
            "(SELECT user_id FROM user_info WHERE user_email = ?);";

    PreparedStatement preparedStatement = connection.prepareStatement(query);
    preparedStatement.setString(1, email);
    ResultSet resultSet = preparedStatement.executeQuery();

    while (resultSet.next()) {
        posts.add(new Post(resultSet.getString("user_post"),resultSet.getString("post_time")));
    }
    postBundle = new PostBundle("200","SUCCESS!",posts);
    return postBundle;         
}
...

我在这里看到一些错误:

首先,调用
resultSet.next()
两次

// Initially the cursor is positioned before the first row.
ResultSet resultSet = preparedStatement.executeQuery();

if  (resultSet != null && resultSet.next()) { // The cursor is moved to the first row
    while (resultSet.next()) { // The cursor is moved to the second row, so you skipped the
                               // first result and it only iterates from second to last one
        ...
    }
    postBundle = new PostBundle("200","SUCCESS!",posts); // posts only contains from
                                                         //second to last result. In case
                                                         //you only have one result,
                                                         //posts is an empty ArrayList

    return postBundle;
}
“xyz”有多少个帖子。123@test.in“你有吗?如果只有一个,那就是问题所在

Second,
preparedStatement.executeQuery()
从不返回
null

因此,您不需要检查它是否为
null

第三,如果电子邮件中没有任何帖子,则永远不要返回404,而应返回200,其中包含空json。

200确定-请求已成功。返回的信息与 响应取决于请求中使用的方法

404未找到-服务器未找到任何与 请求URI

404错误保留用于请求URI匹配

最后,您应该相应地修改代码:

您应该修改以下代码:

...
try {
    Connection connection = DBConnectionManager.getConnection();
    final String query = "SELECT user_post, post_time FROM wall_post WHERE user_id = " +
            "(SELECT user_id FROM user_info WHERE user_email = ?);";

    PreparedStatement preparedStatement = connection.prepareStatement(query);
    preparedStatement.setString(1, email);
    ResultSet resultSet = preparedStatement.executeQuery();

    while (resultSet.next()) {
        posts.add(new Post(resultSet.getString("user_post"),resultSet.getString("post_time")));
    }
    postBundle = new PostBundle("200","SUCCESS!",posts);
    return postBundle;         
}
...

建议的解决方案对您有效吗?如果是,你能接受这个解决方案吗?如果没有,你能告诉我们什么仍然不起作用吗?建议的解决方案对你有效吗?如果是,你能接受这个解决方案吗?如果没有,你能告诉我们什么仍然不起作用吗?