Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring Boot中使用aws s3进行事务处理_Java_Spring_Spring Boot_Jpa - Fatal编程技术网

Java 如何在Spring Boot中使用aws s3进行事务处理

Java 如何在Spring Boot中使用aws s3进行事务处理,java,spring,spring-boot,jpa,Java,Spring,Spring Boot,Jpa,我使用java的spring boot编写了图像上传方法,如下所示。 它在上传名为Todo类的图像之前保存标题和图像路径等。 @Override public Todo saveTodo(String title, String description, MultipartFile file) { // check if the file is empty if (file.isEmpty()) { throw new IllegalStateException(

我使用java的spring boot编写了图像上传方法,如下所示。
它在上传名为Todo类的图像之前保存标题和图像路径等。

@Override
public Todo saveTodo(String title, String description, MultipartFile file) {
    // check if the file is empty
    if (file.isEmpty()) {
        throw new IllegalStateException("Cannot upload empty file");
    }
    // Check if the file is an image
    if (!Arrays.asList(IMAGE_PNG.getMimeType(), IMAGE_BMP.getMimeType(), IMAGE_GIF.getMimeType(),
            IMAGE_JPEG.getMimeType()).contains(file.getContentType())) {
        throw new IllegalStateException("FIle uploaded is not an image");
    }
    // get file metadata
    Map<String, String> metadata = new HashMap<>();
    metadata.put("Content-Type", file.getContentType());
    metadata.put("Content-Length", String.valueOf(file.getSize()));
    // Save Image in S3 and then save Todo in the database
    String path = String.format("%s/%s", amazonConfig.getBucketName(), UUID.randomUUID());
    String fileName = String.format("%s", file.getOriginalFilename());

    Todo todo = Todo.builder().description(description).title(title).imagePath(path).imageFileName(fileName)
            .build();
    repository.save(todo);

    try {
        fileStore.upload(path, fileName, Optional.of(metadata), file.getInputStream());
    } catch (IOException e) {
        throw new IllegalStateException("Failed to upload file", e);
    }

    return repository.findByTitle(todo.getTitle());
}
@覆盖
公共Todo saveTodo(字符串标题、字符串描述、多部分文件){
//检查文件是否为空
if(file.isEmpty()){
抛出新的IllegalStateException(“无法上载空文件”);
}
//检查文件是否为图像
如果(!Arrays.asList(IMAGE_PNG.getMimeType()、IMAGE_BMP.getMimeType()、IMAGE_GIF.getMimeType(),
IMAGE\u JPEG.getMimeType()).contains(file.getContentType()){
抛出新的非法状态异常(“上传的文件不是图像”);
}
//获取文件元数据
映射元数据=新建HashMap();
metadata.put(“内容类型”,file.getContentType());
put(“Content-Length”,String.valueOf(file.getSize());
//在S3中保存图像,然后在数据库中保存Todo
字符串路径=String.format(“%s/%s”,amazonfig.getBucketName(),UUID.randomUUID());
字符串文件名=String.format(“%s”,file.getOriginalFilename());
Todo Todo=Todo.builder().description(description).title(title).imagePath(path).imageFileName(fileName)
.build();
保存(todo);
试一试{
fileStore.upload(路径、文件名、可选的.of(元数据)、file.getInputStream());
}捕获(IOE异常){
抛出新的IllegalStateException(“上传文件失败”,e);
}
返回repository.findbytle(todo.getTitle());
}
但是当上传时发生错误,我想完全删除插入的todo。
我可以使用catch中的repository delete方法手动删除。
但是我可以在不删除只写@transacion之类的方法的情况下执行此操作吗

感谢您阅读我的问题。

只需使用
@Transactional
注释
saveTodo
方法即可

Itt的外观如下所示:

import org.springframework.transaction.annotation.Transactional;

// other imports
// other code 

@Override
@Transactional
public Todo saveTodo(....

然后,如果发生RuntimeException,它将回滚。(IllegalStateException是一个RuntimeException,所以它可以正常工作)

谢谢你,塔马斯。它工作得很好!