gpt4 book ai didi

java - 使用 Spring MVC 显示 XML 字符串

转载 作者:行者123 更新时间:2023-12-01 04:22:23 26 4
gpt4 key购买 nike

我正在尝试向浏览器呈现 XML 文档,但出现黑屏。但是,当我查看页面源代码时,我可以看到 XML。

@RequestMapping(value = "view-xml", method = {RequestMethod.GET})
public ResponseEntity<String> viewXmlPayload(@RequestParam ("id") int taskId){

payload = dao.getXmlPayload(taskId);

HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.TEXT_XML);
return new ResponseEntity<String>(payload, responseHeaders, HttpStatus.OK);
}

使用一些浏览器工具,我可以看到内容类型已正确设置为“text/xml”,但我仍然在页面上看不到任何内容。

最佳答案

来自RFC 3023

If an XML document -- that is, the unprocessed, source XML document -- is readable by
casual users, text/xml is preferable to application/xml. MIME user agents (and web user
agents) that do not have explicit support for text/xml will treat it as text/plain, for
example, by displaying the XML MIME entity as plain text. Application/xml is preferable when the XML MIME entity is unreadable by casual users.

这可能不是您的代码问题,某些浏览器根本不呈现 XML。您需要使用 application/xml 而不是 text/xml

为什么要尝试将 XML 文档渲染到浏览器,如果仍想渲染,请使用 XSLTXML 转换为 HTML.

关于java - 使用 Spring MVC 显示 XML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18785338/

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