Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ??

like image 257
Nikunj Chauhan Avatar asked Aug 04 '11 10:08

Nikunj Chauhan


People also ask

What is @resource in Spring boot?

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.

What is @resource annotation in Spring?

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.


1 Answers

You can create a ByteArrayResource from the String:

String xml = document.getContent();
Resource resource = new ByteArrayResource(xml.getBytes());
setResource(resource);
like image 70
skaffman Avatar answered Sep 20 '22 12:09

skaffman