如何在从angular到spring boot api的发布请求中发送字符串和xlsx.workbook文件?

问题描述

我想用要求它的用户的邮件发送一个excel文件。我有一个有角度的前端和一个弹簧靴后端。

当我尝试发送帖子时,出现“ jsonMappingException:无法构造实例”异常。

我对excel函数的角度构造

const data = document.getElementById('epltable');
const options = {
        background: 'white',scale: 3
};
const ws: xlsx.WorkSheet = xlsx.utils.table_to_sheet(data);
const wb: xlsx.WorkBook = xlsx.utils.book_new();
xlsx.utils.book_append_sheet(wb,ws,'Sheet1');
this.uploadService.sendRecordHistory(wb,this.user.id,this.user.email)
.subscribe(() => {
          console.log('history sent');
});

我的角度后置功能

sendRecordHistory(file: xlsx.WorkBook,userId: number,mail: string) {
    let object: RecordHistoryForSend = {mail,file};
    return this.http.post(`${environment.apiurl}user/` + userId + '/sendRecordHistory',object);
}

我的Java控制器

@PostMapping(value = { "/user/{userId}/sendRecordHistory" })
public ResponseEntity<?> sendRecordHistory(@RequestBody RecordHistoryAndMailDTO record) throws MessagingException {
        this.emailService.sendRecordHistory(record.getMail(),record.getFile());
        return new ResponseEntity<>(HttpStatus.OK);
}

我的邮件服务

@Service
public class EmailService {

    @Autowired
    private JavaMailSender javaMailSender;
    
    @Autowired
    private EmailProperties emailProperties;

@Async
    public void sendRecordHistory(String mail,Workbook excelFile) throws MessagingException {
        MimeMessage msg = javaMailSender.createMimeMessage();
        
        msg.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));
        msg.setFrom(emailProperties.getFrom());

        msg.setSubject("mail Subject");
        msg.setText(
                "text to send");
        msg.setContent((Multipart) excelFile);

        javaMailSender.send(msg);
    }
}

错误

org.springframework.http.converter.HttpMessageConversionException:类型定义错误:[简单类型,类org.apache.poi.ss.usermodel.Workbook];嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造org.apache.poi.ss.usermodel.Workbook的实例(不存在创建者,如默认构造一样):抽象类型要么需要映射到具体类型,要么具有自定义反序列化器,或包含其他类型信息

Here is the payload that confirm that my post request contain the file

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)