I have a class with a resource property of type Resource in spring (org.springframework.core.io.Resource
) which takes a file object as input.
setResource(Resource resource)
{
this.resource = resource;
}
However, I am reading a remote document through another custom API which returns the contents of the document as a String.
String xml = document.getContent();
I want to pass this xml as Resource
in my setResource
method. However, I don't know how can I cast String into Resource
.
Any ideas ??
Spring's Implementations for Resource Interface URLResource: Represents a resource loaded from a URL. ClassPathResource: Represents a resource loaded from the classpath. FileSystemResource: Represents a resource loaded from the filesystem. ServletContextResource: This implementation is for.
The @Resource annotation in spring performs the autowiring functionality. This annotation follows the autowire=byName semantics in the XML based configuration i.e. it takes the name attribute for the injection.
You can create a ByteArrayResource
from the String:
String xml = document.getContent();
Resource resource = new ByteArrayResource(xml.getBytes());
setResource(resource);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With