Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return xml file from spring MVC controller

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

like image 385
Rat Avatar asked Jul 22 '15 13:07

Rat


People also ask

How to return XML from Spring REST controller?

Your controller class must either be annotated with @RestController or with @Controller and @ResponseBody on the method returning the XML. Next, we must declare on the @RequestMapping annotation that out endpoint response will be in XML. It is done by setting the mime type aka media type of the produces parameter.

How to return XML response in REST?

For obtaining an XML response, we have to pass the parameter ContentType. XML to the accept method. We shall first send a GET request via Postman on a mock API URL. Using Rest Assured, we shall validate its XML Response containing the name of the subjects Rest Assured, Postman, and their prices 10 and 6 respectively.

CAN REST service return XML?

Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.


1 Answers

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 
}
like image 176
MichaelCleverly Avatar answered Oct 19 '22 23:10

MichaelCleverly