首页 > 解决方案 > 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();

预期结果:显示所有家具项目。实际结果:显示第一个家具项目。

标签: javajsonrestweb-services

解决方案


显示响应类代码可能更清楚..


推荐阅读