无法在Spring AWS示例中强制转换为org.springframework.core.io.WritableResource [英] cannot be cast to org.springframework.core.io.WritableResource on Spring AWS example

查看:146
本文介绍了无法在Spring AWS示例中强制转换为org.springframework.core.io.WritableResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读本文档:

http://cloud.spring.io/spring -cloud-aws/spring-cloud-aws.html

在Spring应用程序中使用AWS.我对S3特别感兴趣,因此,我设置了应用程序并复制了以下代码片段,以确保设置正确进行:

on using AWS from a Spring application. I'm particularly interested in S3, so, I set up the application and copied this snippet of code to make sure the set up is working correctly:

Resource resource = this.resourceLoader.getResource("s3://myBucket/rootFile.log");
WritableResource writableResource = (WritableResource) resource;
try (OutputStream outputStream = writableResource.getOutputStream()) {
  outputStream.write("test".getBytes());
}

但是当我运行它时,出现此错误:

but when I run it, I get this error:

java.lang.ClassCastException: org.springframework.web.context.support.ServletContextResource cannot be cast to org.springframework.core.io.WritableResource

有什么想法吗?这是安装问题吗?在我看来,它看起来并不像,但这是我的新手.

Any ideas what's wrong? is that a setup problem? It doesn't look like to me, but I'm new to this.

推荐答案

我必须删除

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

来自我的依赖项.

当包含devtools时,它在GenericApplicationContext.getResources()方法内部执行if块,但是在没有devtools的情况下,它将调用super.getResource():

When devtools is included, inside the GenericApplicationContext.getResources() method it executes the if block, but without devtools it calls the super.getResource():

public Resource getResource(String location) {
    if (this.resourceLoader != null) {
        return this.resourceLoader.getResource(location);
    }
    return super.getResource(location);
}

这将使用s3协议处理程序,并为您提供另一种实现WritableResource的资源.

This will use the s3 protocol handler and give you a different type of Resource that does implement WritableResource.

这篇关于无法在Spring AWS示例中强制转换为org.springframework.core.io.WritableResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆