实体类转化为VO返回给前端的方法

返回给前端的时候,我以前一直都是直接用实体类entity直接返回的。但是到后面我越来越发现这样子很不方便:

  1. 比如这个实体类中有许多属性我不想返回给前端,那我只好写个null;
  2. 我要一次查询多张表,返回许多数据的话,就肯定只能多去封装一个大类,也就是VO类

其实这里的方法有几种,最普通的及时一个个set了:

以遍历集合为例:
①遍历出来后取出每个元素,一个个set后再添加到新的集合里

		//普通类型
        List<PhoneCategory> phoneCategoryList = phoneCategoryRepository.findAll();
        //常规写法
        List<PhoneCategoryVO> phoneCategoryVOList = new ArrayList<>();
        for (PhoneCategory phoneCategory : phoneCategoryList) {
            PhoneCategoryVO phoneCategoryVO = new PhoneCategoryVO();
            phoneCategoryVO.setCategoryName(phoneCategory.getCategoryName());
            phoneCategoryVO.setCategoryType(phoneCategory.getCategoryType());
            phoneCategoryVOList.add(phoneCategoryVO);
        }

②用Java8的lambda表达式:

	List<PhoneCategoryVO> phoneCategoryVOList =  phoneCategoryList.stream()
            .map(e -> new PhoneCategoryVO(
                      e.getCategoryName(),
                      e.getCategoryType()
              )).collect(Collectors.toList());

③使用commons-lang3或者直接org.springframework.beans. BeanUtils的copyProperties方法:

 	List<PhoneInfoVO> phoneInfoVOList = new ArrayList<>();
 	for (PhoneInfo phoneInfo : phoneInfoList) {
	      PhoneInfoVO phoneInfoVO = new PhoneInfoVO();
	       //将phoneSpecs中与phoneSpecsVO属性相同的进行拷贝给VO
	      BeanUtils.copyProperties(phoneInfo,phoneInfoVO);
	      //如果还有不同进行手动赋值
	      phoneInfoVO.setTag(PhoneUtil.createTag(phoneInfo.getPhoneTag()));
	      phoneInfoVOList.add(phoneInfoVO);
 	 }

注意如果实体类中有不想赋值的属性,可以使用copyProperties(Object source, Object target, String... ignoreProperties),后面加上忽略的名字;

上面的方法对应的lambda式为:

	List<PhoneInfoVO> phoneInfoVOList = phoneInfoList.stream()
                .map(e -> new PhoneInfoVO(
                        e.getPhoneId(),
                        e.getPhoneName(),
                        e.getPhonePrice()+".00",
                        e.getPhoneDescription(),
                        PhoneUtil.createTag(e.getPhoneTag()),
                        e.getPhoneIcon()
                )).collect(Collectors.toList());

可以看出在这种情况下,lambda表达式要手动的赋值,而使用BeanUtils的copyProperties方法就可以简洁。不过如果说返回给前端的VO和实体类的属性并不一样的话,使用lambda表达式就更加方便了。

当然,我对于lambda表达式还不够熟练,这个就只能多练习了!

  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以定义一个实体类,将文件的相关信息(如文件名、文件类型、文件内容等)作为该实体类的属性,并在该实体类中实现方法将文件内容读取并存储到实体类的属性中。然后在控制器中调用该方法,将返回实体类对象通过响应体返回前端。 示例代码: ```java import org.springframework.util.StreamUtils; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; public class FileEntity { private String fileName; private String fileType; private byte[] fileContent; public FileEntity(MultipartFile file) throws IOException { this.fileName = file.getOriginalFilename(); this.fileType = file.getContentType(); InputStream inputStream = file.getInputStream(); this.fileContent = StreamUtils.copyToByteArray(inputStream); } public String getFileName() { return fileName; } public String getFileType() { return fileType; } public byte[] getFileContent() { return fileContent; } } ``` 在控制器中: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @RestController public class FileController { @PostMapping("/upload") public ResponseEntity<FileEntity> upload(@RequestParam("file") MultipartFile file) throws IOException { FileEntity fileEntity = new FileEntity(file); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileEntity.getFileName()); return new ResponseEntity<>(fileEntity, headers, HttpStatus.OK); } } ``` 在上面的示例代码中,上传文件的请求将会被映射到 /upload 路径。在 upload 方法中,我们将上传的文件换成 FileEntity 类对象,并将该对象作为响应体返回前端。注意,在响应头中添加了 Content-Disposition 属性,告诉浏览器该响应体是一个附件,浏览器应该以下载的方式处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值