SSM 前后分离 跨域请求

本文介绍了一种通过Java实现的产品数据查询与处理方法,包括获取所有产品信息、未上架及已上架产品的详情,并统计了各类产品的数量。此外,还展示了如何使用JSON格式返回查询结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@RequestMapping("/findAllProduct")
    @ResponseBody
    public Object findAllProduct(String callback) {
        List<ProductInfo> productList = productService.findAllProduct();
        for (ProductInfo productInfo : productList) {
            productInfo.setSpecName(productService.findSpecById(productInfo.getProduct_unit()).getSpec_name());
         productInfo.setProduct_typeName(productTypeService.findProductTypeById(productInfo.getProduct_type()).getPtype_name());
        }
        /*ArrayList<ProductInfo> arrayList = new ArrayList<>();
        
        for (int i = 0; i < 10; i++) {
            arrayList.add(productList.get(i));
        }
        System.out.println(arrayList.size());*/
        List<ProductType> productTypeList = productTypeService.findAllProductType();
        int allCount = productService.findProductCount();
        int notShelfCount = productService.findProductNotShelf();
        int shelfCount = productService.findProductShelf();
        List<ProductInfo> productListNoShelf = productService.findProductNoShelf();
        for (ProductInfo productInfo : productListNoShelf) {
            productInfo.setSpecName(productService.findSpecById(productInfo.getProduct_unit()).getSpec_name());
             productInfo.setProduct_typeName(productTypeService.findProductTypeById(productInfo.getProduct_type()).getPtype_name());
        }
        List<ProductInfo> productListShelf = productService.findProductIsShelf();
        for (ProductInfo productInfo : productListShelf) {
            productInfo.setSpecName(productService.findSpecById(productInfo.getProduct_unit()).getSpec_name());
             productInfo.setProduct_typeName(productTypeService.findProductTypeById(productInfo.getProduct_type()).getPtype_name());
        }
        /*ArrayList<Object> list = new ArrayList<>();
        list.add(productList);
        list.add(productListShelf);
        list.add(productListNoShelf);
        list.add(productTypeList);
        list.add(allCount);
        list.add(shelfCount);
        list.add(notShelfCount);*/
        JsonNodeFactory factory = new JsonNodeFactory(false);
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode obj= factory.objectNode();
        try {
             String jsonList1 = mapper.writeValueAsString(productList);
             String jsonList2 = mapper.writeValueAsString(productListShelf);
             String jsonList3 = mapper.writeValueAsString(productListNoShelf);
             String jsonList4 = mapper.writeValueAsString(productTypeList);
             String jsonList5 = mapper.writeValueAsString(allCount);
             String jsonList6 = mapper.writeValueAsString(shelfCount);
             String jsonList7 = mapper.writeValueAsString(notShelfCount);
             obj.put("code", "0");
             obj.put("msg", "成功!");
             obj.put("productList", jsonList1);
             obj.put("productListShelf", jsonList2);
             obj.put("productListNoShelf", jsonList3);
             obj.put("productTypeList", jsonList4);
             obj.put("allCount", jsonList5);
             obj.put("shelfCount", jsonList6);
             obj.put("notShelfCount", jsonList7);
        } catch (Exception e) {
            obj.put("code", "1");
            obj.put("msg", "失败!");
            obj.put("data", "[]");
            e.printStackTrace();
        }
        
        MappingJacksonValue jacksonValue = new MappingJacksonValue(obj);
        jacksonValue.setJsonpFunction(callback);
        return jacksonValue;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值