[英]Spring Data Rest Custom Controller With with content-Type:“text/plain”
[英]Spring MVC 3 Return Content-Type: text/plain
我想在頁面上顯示簡單的文本,因此我想將Content-Type
作為text/plain
。
使用下面的代碼,我在頁面上看到純文本,但返回的Content-Type
仍然是text/html
。
我怎樣才能解決這個問題?
注意:我正在使用帶有Spring MVC的Tiles。 返回的“m.health”指向一個tile def,它映射到一個health.jsp,它只包含下面的1行。
更新說明 :我無法控制HTTP標頭請求中的Content-Type
或Accept
值。 無論發出什么樣的請求,我都希望我的回復能夠返回text/plain
。
控制器:
@RequestMapping(value = "/m/health", method = RequestMethod.GET, headers = "Accept=*")
public String runHealthCheck(HttpServletResponse response, HttpServletRequest request, Model model) throws Exception {
model = executeCheck(request, response, TEMPLATE, false, model);
model.addAttribute("accept", "text/plain");
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
return "m.health";
}
JSP:
$ {}狀態
如果您使用@ResponseBody另外注釋您的方法,它應該有效:
@RequestMapping(value = "/",
method = RequestMethod.GET)
@ResponseBody
public String plaintext(HttpServletResponse response) {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
return "TEXT";
}
您可以嘗試使用text/plain
設置@RequestMapping
注釋的produces
值。 Spring文檔將此列為示例 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.