首页 > 解决方案 > 在 Java 中返回多个响应

问题描述

我有一个 java 方法,它从表 A(带有 BLOB)读取,将某些数据写入表 B,最后将 BLOB 上传到文件服务器。这是代码:

public void insertAndUploadService() throws Exception {

    List<Tiedosto> tiedostoList = new ArrayList<>();
    Integer newRow = 0;
    boolean uploaded;

    try {
        tiedostoList = tiedostoService.getAllFiles();
    } catch (Exception e) {
        e.printStackTrace();
    }

    for (Tiedosto tiedosto : tiedostoList){
        Attachment attachment = new Attachment();
        attachment.setCustomerId(tiedosto.getCustomerId());
        attachment.setSize(tiedosto.getFileSize());

        try {
            newRow = attachmentService.createNew(attachment);
            System.out.println("************ NEW ROW CREATED IN attachments ******************************");
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (newRow == 1 && tiedosto.getContent() != null){
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tiedosto.getContent());
            minioFileServer.uploadFile("test", attachment.getUuid(), tiedosto.getFileName().substring(tiedosto.getFileName().lastIndexOf("/") + 1), byteArrayInputStream,
                    byteArrayInputStream.available(), tiedostoService.getMimeType(tiedosto.getContent()));
            System.out.println("********************** Generating Upload Link ************************************ "+"\n"+
                    minioFileServer.getUploadLinkForFile("test", attachment.getUuid(), tiedosto.getFileName().substring(tiedosto.getFileName().lastIndexOf("/") + 1) ));
            uploaded = minioFileServer.fileExists("test", attachment.getUuid(), tiedosto.getFileName().substring(tiedosto.getFileName().lastIndexOf("/") + 1));

            if (uploaded == true){
                System.out.println("*********** UPLOAD SUCCESSFUL ***************");
                tiedostoService.updateService(tiedosto.getCustomerId(), tiedosto.getId());
                attachmentService.update(attachment.getUuid());
            }
        }

    }
}

如果我向 API 端点发送 POST 请求,例如:

@POST
@Path("attachments")
public void moveToMinio() throws Exception {
    MigrationService migrationService = new MigrationService();
    migrationService.insertAndUploadService();
}

它根据需要完成工作并打印出详细信息。发送 POST 请求时,是否有可能从 insertAndUploadService 发送多个响应?例如当 newRow 创建并上传 == true 时,是否可以返回 HTTP 202?我是 Java 新手,非常感谢任何形式的帮助/建议!

编辑:“多重回应”可能不是正确的术语。我想要返回基于请求操作选择的几个 HTTP 状态代码之一的选项。

标签: javajax-rs

解决方案


推荐阅读