Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

java.lang.ClassCastException while implementing writeGcs() as given example in https://github.com/spring-cloud/spring-cloud-gcp/blob/master/spring-cloud-gcp-examples/spring-cloud-gcp-storage-resource-example/src/main/java/com/example/WebController.java #254

Closed
pankajnd20 opened this issue Dec 7, 2017 · 11 comments

Comments

@pankajnd20
Copy link

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

error line :
try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream())

full code :

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;

@value("gs://mybucketname/my-file.txt")
private Resource gcsFile;

public String readGcsFile() throws IOException {
return StreamUtils.copyToString(
this.gcsFile.getInputStream(),
Charset.defaultCharset()) + "\n";
}

String writeGcs(String data) throws IOException {
    try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream()) {
        os.write(data.getBytes());
    }
    return "file was updated\n";
}

why it's casting super class to sub class in the example ?

@meltsufin
Copy link
Contributor

It looks like you're missing the Google Cloud Storage Spring Boot Starter dependency and the gs://mybucketname/my-file.txt URI is being interpreted as a ServletContextResource rather than the GoogleStorageResource. Hence, you're unable to cast to WritableResource, which is not implemented by ServletContextResource.

Does your pom.xml contain this dependency?

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-storage</artifactId>
</dependency>

The reason we have to cast is because @value currently only works with the Resource types, not WritableResource. See this issue on the JIRA tracker: https://jira.spring.io/browse/SPR-10656.

@pankajnd20
Copy link
Author

Already added the mentioned dependency in pom.xml. Also checked library exist in project or not .
Though getting this error.

@meltsufin
Copy link
Contributor

Can you share a complete example that reproduces this? Maybe change our example to match your setup.

@saturnism
Copy link
Contributor

@joaoandremartins
Copy link
Contributor

Also, in your example you don't seem to be writing to the resource.

@joaoandremartins
Copy link
Contributor

joaoandremartins commented Dec 14, 2017

Please reopen if you are still seeing this issue.

@saurabhguptasg
Copy link

Hi all, this issue is still occurring
with the storage and starter dependencies both included, the resource gets created as a ServletContext resource and throws a cast exception for WritableResource when invoking:
OutputStream storageOutputStream = ((WritableResource)storageObject).getOutputStream();
the sample referenced in this bug has now moved to:
https://github.com/spring-cloud/spring-cloud-gcp/blob/master/spring-cloud-gcp-samples/spring-cloud-gcp-storage-resource-sample/src/main/java/com/example/WebController.java

@joaoandremartins
Copy link
Contributor

@saurabhguptasg when you run the code sample, is the resource created as ServletContextResource?

@sayi65
Copy link

sayi65 commented Jun 10, 2018

i get the same issue.
I wrote the same source as the example

error:
org.springframework.web.context.support.ServletContextResource cannot be cast to org.springframework.core.io.WritableResource

@joaoandremartins
Copy link
Contributor

There is a working Storage code sample in https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-storage-resource-sample.

Would you mind trying it and then adapting to reproduce the issue you're seeing? Please let us know when you were able to do so.

@sayi65
Copy link

sayi65 commented Jun 12, 2018

@joaoandremartins
OK thank you for your comment

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

6 participants