如何通过邮递员测试spring boot多个文件上传及其详细信息?

时间:2019-04-14 08:32:28

标签: java spring-boot

我有一个模型,该模型将用于上传带有文件详细信息的多个文件。

我将收到3份出生证明,公民身份和另一份文件。这三个文件都有自己的详细信息,应该由我的Certificates.java类保存。

BuyerSellerDocs.java

@AllArgsConstructor
@NoArgsConstructor
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class BuyerSellerDocs {

    Long transactionId;
    Long contactTypeId;
    Long contactId;

    Certificates birthDayCertificates;
    Certificates citizenshipCertificates;
    Certificates otherCertificates;


}

对应的子类是

@AllArgsConstructor
@NoArgsConstructor
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Certificates {
    Long documentTypeId;
    String certificateRefno;
    Date issueDate;
    MultipartFile documentFile;
    Long documentType;
}

并且控制器类为

private static  final String[] certificatesType = new String[]{"birthCertificate","citizenCertificate","otherCertificate"};

    @PostMapping("/app/uploadcontactdocuments")
    public ResponseEntity<Tbldocumentdetail> uploadContactDocuments(@ModelAttribute BuyerSellerDocs documents) throws IOException {

        //creating an instance of entity to be saved
        Tbldocumentdetail tbldocumentdetail = new Tbldocumentdetail();
        Certificates certificates = null;

        for(int i=0; i>certificatesType.length; i++) {
           if(certificatesType[i].equalsIgnoreCase("birthCertificate")) {
               certificates = documents.getBirthDayCertificates();
           }
           else  if(certificatesType[i].equalsIgnoreCase("citizenCertificate")) {
               certificates = documents.getCitizenshipCertificates();
           }
           else {
               certificates = documents.getOtherCertificates();
           }

            //finding the type of document (i.e birth certificate, citizenship certificate etc) and setting it to entity
            Tbldocument tbldocument =documentListService.findDocumentbyID(certificates.getDocumentTypeId());
            tbldocumentdetail.setTbldocument(tbldocument);

            //fetch multipart file convert it to bytes and set to entity
            tbldocumentdetail.setDocument(certificates.getDocumentFile().getBytes());

            tbldocumentdetail.setContactid(documents.getContactId());

            //find contact type
            Tblcontacttype tblcontacttype = new Tblcontacttype();
            Optional<Tblcontacttype> tblcontacttype1 =contactTypeRepo.findById(documents.getContactTypeId());
            if(tblcontacttype1.isPresent()){
                tblcontacttype = tblcontacttype1.get();
            }

            //set contact type to document detail
            tbldocumentdetail.setTblcontacttype(tblcontacttype);

            tbldocumentdetail.setReferenceno(documents.getTransactionId());
            java.sql.Date currentDate = new java.sql.Date(new Date().getTime());
            tbldocumentdetail.setDateofupload(currentDate);


            Tbldocumentformat tbldocumentformat = new Tbldocumentformat();
            if(certificates.getDocumentFile().getOriginalFilename().contains("jpg"))
            {
                tbldocumentdetail.setDocumentformat(5);
            } else if(certificates.getDocumentFile().getOriginalFilename().contains("png")){
                tbldocumentdetail.setDocumentformat(1);
            }else if(certificates.getDocumentFile().getOriginalFilename().contains("doc")){
                tbldocumentdetail.setDocumentformat(4);
            }else if(certificates.getDocumentFile().getOriginalFilename().contains("pdf")){
                tbldocumentdetail.setDocumentformat(6);
            }

            tbldocumentdetail.setDocumentname(certificates.getDocumentFile().getOriginalFilename());

            Tbldocumentdetail details =documentDetailsRepo.save(tbldocumentdetail);
        }



//i need to update here
        return ResponseEntity.ok(null);

    }

控制器类不完整。这里的困惑是,如果这是我应该接收参数的方式,如何从邮递员那里调用此api进行测试?

0 个答案:

没有答案