String 将字符串转换为Spring资源的方法
我在spring中有一个资源属性类型为resource的类(String 将字符串转换为Spring资源的方法,string,spring,resources,String,Spring,Resources,我在spring中有一个资源属性类型为resource的类(org.springframework.core.io.resource),它接受一个文件对象作为输入 setResource(Resource resource) { this.resource = resource; } 但是,我正在通过另一个自定义API读取远程文档,该API以字符串形式返回文档的内容 String xml = document.getContent(); 我想在我的setResource方
org.springframework.core.io.resource
),它接受一个文件对象作为输入
setResource(Resource resource)
{
this.resource = resource;
}
但是,我正在通过另一个自定义API读取远程文档,该API以字符串形式返回文档的内容
String xml = document.getContent();
我想在我的setResource
方法中将此xml作为Resource
传递。但是,我不知道如何将字符串强制转换到资源中
任何想法???您可以从以下字符串创建ByteArrayResource
:
String xml = document.getContent();
Resource resource = new ByteArrayResource(xml.getBytes());
setResource(resource);
谢谢这有助于。。为什么不建议使用setResource(新的InMemoryResource(xml))?@Nikunj:因为InMemoryResource
不是Spring的一部分,它是Spring安全性的一部分。如果你有,那么它会很好。谢谢你的澄清!!