【问题标题】:REST not returning all the values in my DBREST 没有返回我的数据库中的所有值
【发布时间】:2016-08-16 22:01:12
【问题描述】:

大家好,我在返回休息响应时没有获得数据库中的所有值。

下面是我的 DAO

public class BooksDAO {

public List<Book> getAllBooks() throws SQLException, ClassNotFoundException {

    List<Book> arrBook = null;
    Book e = null;
    Class.forName("com.mysql.jdbc.Driver");
    JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
    rowSet.setUrl("jdbc:mysql://localhost:3306/books");
    rowSet.setUsername("xxxx");
    rowSet.setPassword("xxxx");
    rowSet.setCommand("select * from books.library");
    rowSet.execute();

    while (rowSet.next()) {

        e = new Book(rowSet.getInt(1), rowSet.getString(2), rowSet.getString(3), rowSet.getString(4));
        arrBook = new ArrayList<Book>();
        arrBook.add(e);

    }

    return arrBook;

}

以下是我的服务

public class BookService {

BooksDAO booksDao = new BooksDAO();

//private Map<Integer, Book> books = Database.getBooks();

/*public BookService() {
    books.put(1, new Book(1, "The Adventures of Tom Sawyer", "$28", "Mark Twain"));
    books.put(2, new Book(2, "The Adventures of Sherlock Holmes", "$17", "Arthur Conan Doyle"));
}*/

public List<Book> getAllBooks() throws SQLException, ClassNotFoundException {

    return new ArrayList<Book>(booksDao.getAllBooks());

}

下面是我的资源

@GET
@Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8"})
public Response getBooks(@QueryParam("format") String format) throws SQLException, ClassNotFoundException {

    return Response.status(Status.OK).entity((new GenericEntity<List<Book>>(bookService.getAllBooks()) {
    })).header(HttpHeaders.CONTENT_TYPE, "XML".equalsIgnoreCase(format)
            ? MediaType.APPLICATION_XML + ";charset=UTF-8" : MediaType.APPLICATION_JSON + ";charset=UTF-8").build();

}

我收到以下 REST 响应

[
  {
    "author": "Arthur Conan Doyle",
    "id": 2,
    "links": [],
    "name": "The Adventures of Sherlock Holmes",
    "price": "$17"
  }
]

它正在返回最后一个但不是整个列表...

我在哪里做错了?

谢谢

【问题讨论】:

    标签: java mysql rest


    【解决方案1】:

    在您的BookDao#getAllBooks 方法实现中,您正在重构ArrayList,该ArrayList 在遍历您的ResultSet 的同一循环中保存每个元素的结果:

    while (rowSet.next()) {
      e = new Book(rowSet.getInt(1), rowSet.getString(2), rowSet.getString(3),rowSet.getString(4));
      arrBook = new ArrayList<Book>();
      arrBook.add(e);
    }
    

    在循环遍历ResultSet 元素之前,该列表应该被初始化一次:

    arrBook = new ArrayList<Book>();
    while (rowSet.next()) {
      e = new Book(rowSet.getInt(1), rowSet.getString(2), rowSet.getString(3),rowSet.getString(4));
      arrBook.add(e);
    }
    

    【讨论】:

      【解决方案2】:

      尝试如下修改您的代码:

      arrBook = new ArrayList<Book>();                
      
      while (rowSet.next()) {
              e = new Book(rowSet.getInt(1), rowSet.getString(2), rowSet.getString(3), rowSet.getString(4));
              arrBook.add(e);
          }
      

      您正在为每本书初始化您的 arrBook。这会创建一个新的数组列表并丢弃您已经添加的数据。尝试在 while 循环之外初始化 arrBook 数组列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-14
        • 1970-01-01
        • 2022-01-09
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-03
        相关资源
        最近更新 更多