2016-10-20 16 views
0

私はクエリが続き、私は(今だけGET用)すべての要求を処理しクエリクラスを持っている電子メールIDREST APIがネストされたオブジェクトで空のJSONを返すのはなぜですか?

与えHerokuのアプリのデータベースからすべての記事を検索するために

をしようとしています。 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("[email protected]"); 

    } 

} 

は、それから私は、ステータスコードとメッセージを持つすべての記事をバンドルPostManagerと呼ばれるものを持っています。続き

PostManager.java

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; 
    } 

} 

され、最終的に、次はそれがthis site

に空白JSONを返してPost.java

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メソッドでは、エラースタックトレースもバンドルされていることを確認していますが、絶対ブランク画面が表示されます。私はここにいくつかのエラーを参照してください

+0

をあなたのために提案された解決策の作業をしましたか?はいの場合は、解決策を受け入れることができますか?そうでない場合は、まだ動作していないことを教えてください。 – jalv1039

答えて

2

まず、あなたは "[email protected]" メールが持っているどのように多くの記事二回

// 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; 
} 

resultSet.next()を呼びますか?それが1つだけの場合、それは問題です。

第二に、preparedStatement.executeQuery()null

を返さないので、あなたはそれがnullあるかどうかを確認する必要はありませんでした。

第3に、電子メールには404が返されず、代わりに空のjsonで200が返されます。

200 OK - 要求は成功しました。 応答で返される情報は、要求で使用された方法によって異なります。

404が見つかりません - サーバーは、 Request-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;   
} 
... 
関連する問題