【发布时间】: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"
}
]
它正在返回最后一个但不是整个列表...
我在哪里做错了?
谢谢
【问题讨论】: