gpt4 book ai didi

java - 从 spring MVC Controller 传递 XML 文件

转载 作者:行者123 更新时间:2023-11-30 03:20:01 25 4
gpt4 key购买 nike

我是 Spring 新手,我需要将 Spring Controller 的 XML 文件传递​​到我的 JavaScript 文件。谁能详细说明我应该如何做到这一点?我已在我的 Controller 上尝试过此操作,但响应正文没有内容。

@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;
}
}

最佳答案

你正在让事情变得复杂。

@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();
}
}

类似的东西应该可以工作,您可能想要添加更好的异常处理(例如向客户端发送错误)。

关于java - 从 spring MVC Controller 传递 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31558209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com