7

I have tried a lot to return a file from the controller function.

This is my function:

@RequestMapping(value = "/files", method = RequestMethod.GET)
@ResponseBody public FileSystemResource getFile() {
     return new FileSystemResource(new File("try.txt")); 
}

I got this error message:

Could not write JSON:
No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain:
org.springframework.core.io.FileSystemResource[\"outputStream\"]->java.io.FileOutputStream[\"fd\"]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: org.springframework.core.io.FileSystemResource[\"outputStream\"]->java.io.FileOutputStream[\"fd\"])

Does anyone have an idea how to solve it?

And, how should I send from the client (JavaScript, jQuery)?

5
  • What HttpMessageConverter instances have you registered? Commented Jul 22, 2015 at 13:54
  • ammm..., I am new to Spring. Can you explain me? Commented Jul 22, 2015 at 14:01
  • I want to see your MVC configuration. Commented Jul 22, 2015 at 14:04
  • Also tell us which request headers your client sends. Commented Jul 22, 2015 at 14:08
  • On the client I used the simple the jQuery get: jQuery.get("http://localhost:8086/vos-api/StreamProcessingDiagrams/v1/setting/createFOO", function( data ) { jQuery( ".result" ).html( data ); }); Commented Jul 22, 2015 at 14:23

1 Answer 1

7

EDIT 2: First of all - see edit 1 in the bottom - that't the right way to do it. However, if you can't get your serializer to work, you can use this solution, where you read the XML file into a string, and promts the user to save it:

@RequestMapping(value = "/files", method = RequestMethod.GET)
public void saveTxtFile(HttpServletResponse response) throws IOException {

    String yourXmlFileInAString;
    response.setContentType("application/xml");
    response.setHeader("Content-Disposition", "attachment;filename=thisIsTheFileName.xml");

    BufferedReader br = new BufferedReader(new FileReader(new File(YourFile.xml)));
    String line;
    StringBuilder sb = new StringBuilder();

    while((line=br.readLine())!= null){
        sb.append(line);
    }

    yourXmlFileInAString  = sb.toString();

    ServletOutputStream outStream = response.getOutputStream();
    outStream.println(yourXmlFileInAString);
    outStream.flush();
    outStream.close();
}

That should do the job. Remember, however, that the browser caches URL contents - so it might be a good idea to use a unique URL per file.

EDIT:

After further examination, you should also just be able to add the following piece of code to your Action, to make it work:

response.setContentType("text/plain");

(Or for XML)

response.setContentType("application/xml");

So your complete solution should be:

@RequestMapping(value = "/files", method = RequestMethod.GET)
@ResponseBody public FileSystemResource getFile(HttpServletResponse response) {
    response.setContentType("application/xml");
    return new FileSystemResource(new File("try.xml")); //Or path to your file 
}
Sign up to request clarification or add additional context in comments.

12 Comments

That's not how I read the question. I think everybody knows that a ResponseBody in spring returns an XML/JSON body; but OP asks how to return a file. I give an example of that, and I think it's a bit unjustified that you give me minus for that. You should provide a solution for him instead.
I think everybody knows that a ResponseBody in spring returns an XML/JSON body It can provide much more than that.
I really think that you should provide a solution yourself. However - I think my answer to the question "How do you return a file in Spring MVC" is answered with a nifty piece of code - and I hope OP can use it.
MichaelCleverly, first - this worked well, but I need to use an exist file and not to download it, only to get it on ajax request. (and it should be XML...)
I added a small edit. That should be added to your original solution
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.