Web开发-问题-前后端交互数据不一致

0x01 问题描述

  • 所用的技术:Vue+Spring Boot
  • 后端传给前端数据:
cpp 复制代码
[Student(studentId=1, person=org.fatmansoft.teach.models.Person@4abe6020, major=软件工程, className=一班, grade=一年级), Student(studentId=2, person=org.fatmansoft.teach.models.Person@5207b9e6, major=软件工程, className=二班, grade=二年级)]
  • 前端接收数据:

前后端通讯数据明显不同,非常疑惑???(・∀・(・∀・(・∀・*)

0x02 解决

问题原因

javascript 复制代码
export interface StudentItem {
  studentId: number;
  personId: number;
  num: string;
  name: string;
  dept: string;
  major: string;
  className: string;
  card: string;
  gender: string;
  genderName: string;
  birthday: string;
  email: string;
  phone: string;
  address: string;
  grade: string;
  url: string;
  introduce: string;
}
  • 前端的学生数据定义,和后端传的不一致,后端传的person是封装过的,所以前端获取不到person内的数据
  • 要想获取到数据,就要在后端将person里封装的数据给拿到Student下

解决方案

定义一个模板类StudentDto继承Student,将Student中没有的数据(此处是url和name),从person里拿到StudentDto里,然后返回一个StudentDto的list。

java 复制代码
   public DataResponse getStudentListByMajor(@Valid @RequestBody DataRequest dataRequest){
        Optional<Student> student = studentRepository.findStudentByStudentId((Integer) dataRequest.get("studentId"));
        Student s = student.get();
        String major = s.getMajor();
        List<Student> list = studentRepository.findStudentListByMajor(major);
        List<StudentDto> collect = list.stream().map((item) -> {
            StudentDto dto = new StudentDto();
            BeanUtils.copyProperties(item, dto);
            dto.setName(item.getPerson().getName());
            dto.setUrl(item.getPerson().getUrl());
            return dto;
        }).collect(Collectors.toList());
        return CommonMethod.getReturnData(collect);
    }
相关推荐
木西1 天前
Energy-Fi Protocol: 去中心化能源金融协议技术解析
web3·智能合约·solidity
Web3_Daisy1 天前
Flap怎么玩?低门槛 Meme 币的发射与链上策略
大数据·人工智能·web3·区块链·比特币
TechubNews2 天前
從25Q4及全年財報數字看燦谷(Cango Inc)戰略轉向AI
网络·人工智能·web3·区块链
木西3 天前
Energy-Fi:基于 DePIN 的能源资产化协议设计与实现
web3·智能合约·solidity
暴躁小师兄数据学院3 天前
【WEB3.0零基础转行笔记】Go编程篇-第11讲:Gin框架
笔记·golang·web3·区块链·智能合约
奇树谦3 天前
从Web1.0到Web3.0:互联网迭代下工业软件的进化与变革
web3
木西4 天前
深度解析 AgentFi:基于 ERC-6551 与 AI 驱动的 DeFi 进化论
web3·智能合约·solidity
小七mod5 天前
【ETH】以太坊账户模式
web3·区块链·eth·以太坊·比特币·btc·账户模型
Maimai108086 天前
React Server Components 是什么?一文讲清 CSR、Server Components 与 Next.js 中的客户端/服务端组件
前端·javascript·css·react.js·前端框架·html·web3
Lao乾妈官方认证唯一女友:D8 天前
通过plasmo的wallet扩展添加新钱包
javascript·web3·区块链