22

ページに単純なテキストを表示したいので、Content-Typeasを返したいですtext/plain

以下のコードを使用すると、ページにプレーン テキストが表示されますが、戻り値Content-Typetext/html.

どうすればこれを修正できますか?

注:Spring MVC でタイルを使用しています。返された「m.health」は、以下の 1 行のみを含む health.jsp にマップされるタイル定義を指します。

更新注: 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:

${ステータス}

4

2 に答える 2

56

メソッドに @ResponseBody で追加のアノテーションを付けると、うまくいくはずです。

@RequestMapping(value = "/",
                method = RequestMethod.GET)
@ResponseBody
public String plaintext(HttpServletResponse response) {
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");
    return "TEXT";
}
于 2012-03-14T22:11:25.843 に答える
10

注釈のproduces値を。で設定してみることができます。Springのドキュメントには、これがサンプルとしてリストされています。@RequestMappingtext/plain

于 2012-01-21T08:11:31.053 に答える