失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Spring Boot笔记-get请求发送json数据(方便前端vue解析)

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

时间:2019-10-16 07:33:09

相关推荐

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;@Datapublic 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;@Datapublic 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;@Datapublic 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解析)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。