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);
    }
相关推荐
明月_清风20 小时前
Foundry Cheatcode 详解:prank、warp、deal、expectRevert、expectEmit
后端·web3·solidity
明月_清风20 小时前
Foundry Fuzz Testing:让测试自动寻找 Solidity Bug
后端·web3·solidity
明月_清风2 天前
Foundry Web3 测试框架入门:从 0 开始写 Solidity 测试
后端·web3·solidity
Web3李李2 天前
短剧+Web3:破解行业获客留客难题,打造零门槛用户增长闭环
web3·去中心化·区块链·软件开发·dapp开发·web3短剧·web3项目开发
磐链科技5 天前
交易所开发核心:钱包提现引擎的设计——从风控拦截、多级审批到自动广播上链
web3·区块链
磐链科技7 天前
Web3钱包开发中的交易风控体系:模拟执行、沙盒隔离与授权拦截
web3·区块链
磐链科技9 天前
Web3钱包开发扩展比特币生态:BRC20与Runes协议的集成实践
web3
磐链科技10 天前
Web3钱包安全架构设计:抵御攻击与防护策略
安全·web3·安全架构
mykj155113 天前
Web3开发系统不是炒币,是互联网的“对账本”逻辑
web3·区块链·rwa·钱包区块链系统开发
磐链科技16 天前
轻量级与模块化:面向移动端的Web3钱包开发框架选型与技术精简策略
web3