我有一个Sping Boot 项目,它使用jClouds,并有2个类,如下所示:
@Configuration
public class S3Config {
@Value("${amazon.s3.access-key}")
private String accessKey;
@Value("${amazon.s3.secret-key}")
private String secretKey;
@Value("${amazon.s3.region}")
private String region;
@Value("${amazon.s3.bucket-name}")
private String bucketName;
@Bean
public BlobStoreContext context() {
Properties properties = new Properties();
properties.setProperty(S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS, "false");
BlobStoreContext context = ContextBuilder.newBuilder("aws-s3")
.credentials(accessKey, secretKey)
.endpoint("https://s3." + region + ".amazonaws.com")
.overrides(properties)
.buildView(BlobStoreContext.class);
return context;
}
@Bean
public BlobStore blobStore() {
return context().getBlobStore();
}
public void createBucket(String bucketName) {
BlobStoreContext context = context().unwrap();
S3BlobStore s3BlobStore = S3BlobStore.class.cast(context.getBlobStore());
s3BlobStore.createContainerInLocation(null, bucketName);
}
public List<String> listBucket() {
BlobStoreContext context = context().unwrap();
S3BlobStore s3BlobStore = S3BlobStore.class.cast(context.getBlobStore());
PageSet<? extends StorageMetadata> containers = s3BlobStore.list();
List<String> bucketNames = new ArrayList<>();
for (StorageMetadata container : containers) {
bucketNames.add(container.getName());
}
return bucketNames;
}
}
个字符
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gokhan</groupId>
<artifactId>jclouds1mv</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jclouds1mv</name>
<description>jclouds1mv</description>
<properties>
<java.version>17</java.version>
<jclouds.version>2.5.0</jclouds.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds-blobstore</artifactId>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds-allblobstore</artifactId>
<version>${jclouds.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.jclouds/jclouds -->
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds</artifactId>
<version>${jclouds.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
型
当我运行该项目,我得到下面的错误(这是更长的真实的)
我是一个初级开发人员,我不能很好地理解和不能解决这个问题的一段时间。
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'context' defined in class path resource \[com/gokhan/jclouds1mv/config/S3Config.class\]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate \[org.jclouds.blobstore.BlobStoreContext\]: Factory method 'context' threw exception; nested exception is com.google.inject.CreationException: Unable to create injector, see the following errors:
1) \[Guice/ErrorInCustomProvider\]: NoSuchMethodError: 'void ConstructorConstructor.\<init\>(Map)'
at GsonModule.provideGson(GsonModule.java:99)
\_ installed by: AWSS3HttpApiModule -\> GsonModule
at GsonWrapper.\<init\>(GsonWrapper.java:38)
\_ for 1st parameter
at GsonWrapper.class(GsonWrapper.java:32)
while locating GsonWrapper
while locating Json
型
有没有人可以帮这个忙?
在互联网上尝试了一些解决方案,但大多数都过时了。
1条答案
按热度按时间h6my8fg21#
我通过添加以下依赖项解决了这个问题:
根据项目版本更新版本。