我从sql检索数据并存储在列表中。然后,我将其转换为GenericEntity,并尝试使用response.entity()。build();返回它。但是它只返回第一项。
我确认该列表包含所有项目。因此问题应该是列表到实体的转换和/或响应的返回。我尝试循环响应部分,但是没有用。
列表列表=新的ArrayList <>(); 字符串stmt =“”; PreparedStatement ps; 连接conn = 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();
预期结果:显示所有家具。 实际结果:显示第一个家具项目。
答案 0 :(得分:0)
显示Response类的代码可能更清楚。