1

I'm currently writing a REST service with spring boot which should provide a file download, i. e. a client application can download files from the service. A file can be several gigabytes big (sometimes bigger than the main memory), i. e. loading the file into the main memory is not an option. So I need some kind of streaming meachanism when sending a file to the client.

One promising solution on the net (taken from here):

@RestController
// ...
@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
@ResponseBody
public FileSystemResource getFile(@PathVariable("file_name") String fileName) {
    return new FileSystemResource(myService.getFileFor(fileName)); 
}

But it does not work. I get:

java.lang.IllegalStateException: Unsupported resource class: class org.springframework.core.io.FileSystemResource at org.springframework.http.converter.ResourceHttpMessageConverter.readInternal(ResourceHttpMessageConverter.java:100)

I researched but don't know what is causing the issue. I tried other approaches (e. g. Downloading a file from spring controllers) but I get the same error. Does anyone know why the solutions seem to work for others but not for me?

Edit: Here is where I call the REST service (jUnit test case):

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DownloadTest {

    @LocalServerPort
    private int port;

    private RestTemplate template;

    private String requestPath;

    @Before
    public void setUp() throws Exception {
        template = new RestTemplate();
        requestPath = "http://localhost:" + port + "/files/download";
    }

    @Test
    public void test() {
        Resource FileSystemResource =
            template.getForObject(requestPath, FileSystemResource.class);
    }

}
Falco Preiseni
  • 387
  • 4
  • 12
  • 1
    You code seems to work as it for me. Are you sure the `getFile` method is the one throwing this error? I don't even think code is supposed to go through `ResourceHttpMessageConverter.read` as it is doing in your case. Can you post the full stacktrace of the error for clarification. – madteapot Feb 07 '18 at 11:48
  • Thanks for your feedback. Here is the full stack trace: https://paste.gnome.org/pt2jj6oya - I'm calling the rest service in jUnit test case...maybe something is going wrong there. I'll put the call in the main post in a few seconds.... – Falco Preiseni Feb 07 '18 at 12:31
  • Ok, I tested the REST service with curl and it works (`curl --noproxy 127.0.0.1 -i -X GET -H "Content-Type:application/json" -O 'http://127.0.0.1:8080/files/download'`). So the problem is my client/jUnit code. @Setu How did you call the REST service from Java? – Falco Preiseni Feb 07 '18 at 13:04

2 Answers2

0

The code of the RestController is working as expected with a small memory footprint (the file is never loaded to the main memory completely).

The client code that works for me (also with a small memory footprint) can be found here.

Falco Preiseni
  • 387
  • 4
  • 12
0

i fixed the java.lang.IllegalStateException: Unsupported resource error changing this

Resource FileSystemResource = 
            template.getForObject(requestPath, FileSystemResource.class);


for this

  Resource FileSystemResource =
            template.getForObject(requestPath, Resource .class);