java - Response.entity().build 不返回所有结果
问题描述
我从 sql 中检索数据并存储在一个列表中。然后我将其转换为 GenericEntity 并尝试使用 response.entity().build(); 返回它。但它只返回第一项。
我确认列表包含所有项目。所以问题应该是列表到实体的转换和/或响应的返回。我尝试循环响应部分,但没有用。
列表列表 = 新的 ArrayList<>(); 字符串 stmt = ""; PreparedStatement ps; 连接连接 = DriverManager.getConnection("jdbc:mysql://localhost:3306/islandfurniture-it07?zeroDateTimeBehavior=convertToNull&user=root&password=12345");
if (countryID == null) {
stmt = "SELECT i.ID as id, i.NAME as name, f.IMAGEURL as imageURL, i.SKU as sku, i.DESCRIPTION as description, i.TYPE as type, i._LENGTH as length, i.WIDTH as width, i.HEIGHT as height, i.CATEGORY as category FROM itementity i, furnitureentity f where i.ID=f.ID and i.ISDELETED=FALSE and i.CATEGORY=?;";
ps = conn.prepareStatement(stmt);
ps.setString(1, category);
} else {
stmt = "SELECT i.ID as id, i.NAME as name, f.IMAGEURL as imageURL, i.SKU as sku, i.DESCRIPTION as description, i.TYPE as type, i._LENGTH as length, i.WIDTH as width, i.HEIGHT as height, i.CATEGORY as category, ic.RETAILPRICE as price FROM itementity i, furnitureentity f, item_countryentity ic where i.ID=f.ID and i.ID=ic.ITEM_ID and i.ISDELETED=FALSE and ic.COUNTRY_ID=? and i.CATEGORY=?;";
ps = conn.prepareStatement(stmt);
ps.setLong(1, countryID);
ps.setString(2, category);
}
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Furniture f = new Furniture();
f.setId(rs.getLong("id"));
f.setName(rs.getString("name"));
f.setImageUrl(rs.getString("imageURL"));
f.setSKU(rs.getString("sku"));
f.setDescription(rs.getString("description"));
f.setType(rs.getString("type"));
f.setWidth(rs.getInt("width"));
f.setHeight(rs.getInt("height"));
f.setLength(rs.getInt("length"));
f.setCategory(rs.getString("category"));
if (countryID != null) {
f.setPrice(rs.getDouble("price"));
}
list.add(f);
}
GenericEntity<List<Furniture>> entity = new GenericEntity<List<Furniture>>(list) {
};
return Response
.status(200)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
.header("Access-Control-Max-Age", "1209600")
.entity(entity)
.build();
预期结果:显示所有家具项目。实际结果:显示第一个家具项目。
解决方案
显示响应类代码可能更清楚..
推荐阅读
- algorithm - 快速排序分区程序(Lomuto)中的交换条件?
- javascript - 不能将目标属性添加到锚点
- c - The program does not seem to be exiting the loop. Why is that?
- java - 具有 AnnotationConfigApplicationContext 的 Spring 应用程序:获取运行时异常
- javascript - 淡出前的画面变化 - setTimeout 不起作用
- computer-vision - 如何测试 microsoft conitive services 计算机 vsion api?
- c# - 在表中插入记录时,路径不是合法形式,但文件字段中的文件除外
- sql-server - 由于同一记录的列中有多个值,查询返回重复记录。需要帮助过滤查询
- python-3.x - 如何在不舍入或向下转换的情况下将 pandas 字符串变量转换为浮点数?
- laravel - 如何从请求对象 Laravel 中获取属性?