Reputation: 35
I have a Spring Boot project which uses jClouds and has 2 classes as shown below:
@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;
}
}
@RestController
@RequestMapping("/s3")
public class S3Controller {
@Autowired
private S3Config s3Config;
@PostMapping("/create-bucket")
public void createBucket(@RequestParam String bucketName) {
s3Config.createBucket(bucketName);
}
@GetMapping("/list-bucket")
public List<String> listBucket() {
return s3Config.listBucket();
}
}
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>
When I run the project, I get below error ( It is much longer for real)
I am a junior developer, I can not understand well and can not solve this problem for a while.
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
Is there anyone who can help for this please?
Tried some solutions in internet, however most of them out of date.
Upvotes: 1
Views: 1659
Reputation: 21
I solved this issue by adding Following dependencies:
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version> <!-- Update to the latest compatible version -->
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version> <!-- Update to the latest compatible version -->
</dependency>
<!-- Other dependencies -->
update the version based on your project version.
Upvotes: 2