Ways to convert String to Spring Resource
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 ??
You can create a ByteArrayResource
from the String:
String xml = document.getContent();
Resource resource = new ByteArrayResource(xml.getBytes());
setResource(resource);
精彩评论