1

I am new to Spring, and I need to pass from the spring controller an XML file to my JavaScript file. Can anyone detail me how I should do that? I have tried this on my controller, but the Response Body is no content.

@Api(value = "XML", description = "")
@RequestMapping("/XML/v1/setting")
public class XMLController{

    @RequestMapping(method = RequestMethod.GET,
         value = "/createFOO",
         produces = "application/xml")
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public Document createFOO(){
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder;
       Document doc1 = null;
       try {
          builder = factory.newDocumentBuilder();
           doc1 = builder.parse(new FileInputStream("largeXmlGraph.xml"));
       } catch (ParserConfigurationException | SAXException | IOException e) {
                e.printStackTrace();
       }
       return doc1;
    }
}
Jan
  • 2,060
  • 2
  • 29
  • 34
Rat
  • 357
  • 2
  • 5
  • 16

2 Answers2

2

You are making it way to complex.

@RequestMapping(method = RequestMethod.GET,value = "/createFOO",produces = "application/xml")
public void createFOO(OutputStream out){
    try (InputStream is = new FileInputStream("largeXmlGraph.xml")) {
        StreamUtils.copy(is, out);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Something like that should work, you might want to add a bit better exception handling (sending an error to the client for instance).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • `StringUtils.copy()` that's handy. :) – Edward J Beckett Jul 22 '15 at 09:08
  • But, :(, how should I send the OutputStream to the function? – Rat Jul 22 '15 at 09:12
  • I strongly suggest a read on how to use `@RequestMapping` and what method arguments are supported. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-arguments – M. Deinum Jul 22 '15 at 09:20
  • as I understood, I don't need to send this parameter, right? – Rat Jul 22 '15 at 09:34
  • And if I right, why I still have the on Response Body no content? Thanks a lot for your helping – Rat Jul 22 '15 at 09:36
  • 1
    The file is empty, or the file is in a different location. Could be anything but this is basically how to send any file back to the client. Instead of the `OutputStream` you could also use the `HttpServletResponse` and set the appropriate content-type header to `application/xml`. – M. Deinum Jul 22 '15 at 09:38
  • Sorry for the stupid questions, but how should I take values from the call? regular? like: `jQuery.get("/api/XML//createFOO", function( data ) { jQuery( ".result" ).html( data );} );` or in a diffrent way? – Rat Jul 22 '15 at 11:37
0

In order to work with application/xml, add this dependency

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

Reference link to Multiple scenarios @RequestMapping produces JSON/XML together with Accept or ResponseEntity

Denuwan.hh
  • 56
  • 3