【问题标题】:Return xml file from spring MVC controller从 Spring MVC 控制器返回 xml 文件
【发布时间】:2015-10-12 10:25:41
【问题描述】:

我已经尝试了很多方法来从控制器函数中返回一个文件。

这是我的功能:

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

我收到此错误消息:

无法写入 JSON:
没有找到类 java.io.FileDescriptor 的序列化程序,也没有发现创建 BeanSerializer 的属性
(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS))
(通过参考链:
org.springframework.core.io.FileSystemResource[\"outputStream\"]->java.io.FileOutputStream[\"fd\"]);
嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.FileDescriptor and no properties found to create BeanSerializer
(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS))
(通过引用链:org.springframework.core.io.FileSystemResource[\"outputStream\"]->java.io.FileOutputStream[\"fd\"])

有人知道如何解决吗?

而且,我应该如何从客户端发送(JavaScript、jQuery)?

【问题讨论】:

  • 您注册了哪些HttpMessageConverter 实例?
  • ammm...,我是 Spring 新手。你能解释一下吗?
  • 我想看看你的 MVC 配置。
  • 还告诉我们您的客户端发送了哪些请求标头。
  • 在客户端我使用了简单的 jQuery 获取:jQuery.get("http://localhost:8086/vos-api/StreamProcessingDiagrams/v1/setting/createFOO", function( data ) { jQuery( ".result" ).html( data ); });

标签: java xml spring file spring-mvc


【解决方案1】:

编辑 2:首先 - 请参阅底部的编辑 1 - 这不是正确的方法。但是,如果你不能让你的序列化程序工作,你可以使用这个解决方案,你将 XML 文件读入一个字符串,并提示用户保存它:

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

这应该可以完成工作。但是请记住,浏览器会缓存 URL 内容,因此最好为每个文件使用唯一的 URL。

编辑:

经过进一步检查,您还应该能够将以下代码添加到您的 Action 中,以使其工作:

response.setContentType("text/plain");

(或用于 XML)

response.setContentType("application/xml");

所以你的完整解决方案应该是:

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

【讨论】:

  • 这不是我阅读问题的方式。我想每个人都知道 Spring 中的 ResponseBody 返回一个 XML/JSON 正文;但 OP 询问如何返回文件。我举了一个例子,我认为你给我减分有点不合理。你应该为他提供解决方案。
  • 我想每个人都知道 Spring 中的 ResponseBody 返回一个 XML/JSON 主体它可以提供的远不止这些。
  • 我真的认为您应该自己提供解决方案。但是 - 我认为我对“如何在 Spring MVC 中返回文件”这个问题的回答是用一段漂亮的代码来回答的 - 我希望 OP 可以使用它。
  • MichaelCleverly,首先 - 这很好用,但我需要使用现有文件而不是下载它,只是为了在 ajax 请求中获取它。 (它应该是 XML...)
  • 我添加了一个小编辑。这应该添加到您的原始解决方案中
猜你喜欢
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多