日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

Spring Boot笔记-get请求发送json数据(方便前端vue解析)

發布時間:2025/3/15 vue 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot笔记-get请求发送json数据(方便前端vue解析) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

?

?

基本概念

代碼與實例


?

基本概念

這里有一個思路,后端只發送Json數據,前端vue去解析。這樣的話,就可以做到前后端分離,耦合性就很低了。

?

代碼與實例

程序運行截圖如下:

得到后,使用vue去解析,然后頁面顯示。

這里可以使用nginx做個代理,就看不出來了!

結構如下:

這里vo就是存的Json格式

controller就是發送請求的

數據庫結構如下:

product_category

product_info

關鍵源碼如下:

ProductInfoVo.java

package selldemo.demo.vo;import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data;import java.math.BigDecimal;@Data public class ProductInfoVO {@JsonProperty("id")private String productId;@JsonProperty("name")private String productName;@JsonProperty("price")private BigDecimal productPrice;@JsonProperty("description")private String productDescription;@JsonProperty("icon")private String productIcon; }

ProductVO.java

package selldemo.demo.vo;import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data;import java.util.List;@Data public class ProductVO {@JsonProperty("name")private String categoryName;@JsonProperty("type")private Integer categoryType;@JsonProperty("foods")private List<ProductInfoVO> productInfoVOList; }

ResultVO.java

package selldemo.demo.vo;import lombok.Data;@Data public class ResultVO<T> {// 錯誤碼private Integer code;// 提示信息private String msg;// 具體內容private T data; }

BuyerProductController.java

package selldemo.demo.controller;import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import selldemo.demo.dataobject.ProductCategory; import selldemo.demo.dataobject.ProductInfo; import selldemo.demo.service.CategoryService; import selldemo.demo.service.ProductService; import selldemo.demo.vo.ProductInfoVO; import selldemo.demo.vo.ProductVO; import selldemo.demo.vo.ResultVO;import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors;@RestController @RequestMapping("/buyer/product") public class BuyerProductController {@Autowiredprivate ProductService productService;@Autowiredprivate CategoryService categoryService;@GetMapping("/list")public ResultVO list(){//測試用例 // ResultVO resultVO = new ResultVO(); // ProductVO productVO = new ProductVO(); // ProductInfoVO productInfoVO = new ProductInfoVO(); // // productVO.setProductInfoVOList(Arrays.asList(productInfoVO)); // resultVO.setData(productVO); // // resultVO.setCode(0); // resultVO.setMsg("成功"); // // return resultVO;// 1. 查詢所有上架商品List<ProductInfo> productInfoList = productService.findUpAll();// 2. 查詢類目(一次性查詢)/*//傳統方法List<Integer> categoryTypeList = new ArrayList<>();categoryService.findByCategoryTypeIn(categoryTypeList);for(ProductInfo productInfo : productInfoList){categoryTypeList.add(productInfo.getCategoryType());}*///精簡方法java8 lambdaList<Integer> categoryTypeList = productInfoList.stream().map(e->e.getCategoryType()).collect(Collectors.toList());List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypeList);// 3. 數據拼接List<ProductVO> productVOList = new ArrayList<>();for(ProductCategory productCategory : productCategoryList){ProductVO productVO = new ProductVO();productVO.setCategoryType(productCategory.getCategoryType());productVO.setCategoryName(productCategory.getCategoryName());List<ProductInfoVO> productInfoVOList = new ArrayList<>();for(ProductInfo productInfo : productInfoList){if(productInfo.getCategoryType().equals(productCategory.getCategoryType())){ProductInfoVO productInfoVO = new ProductInfoVO();BeanUtils.copyProperties(productInfo, productInfoVO);productInfoVOList.add(productInfoVO);}}productVO.setProductInfoVOList(productInfoVOList);productVOList.add(productVO);}ResultVO resultVO = new ResultVO();resultVO.setData(productVOList);resultVO.setCode(0);resultVO.setMsg("成功");return resultVO;} }

?

總結

以上是生活随笔為你收集整理的Spring Boot笔记-get请求发送json数据(方便前端vue解析)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。