Java – Spring MVC 3 Return Content-Type: text/plain


I want to display simple text on a page and as such I want to return the Content-Type as text/plain.

Using the code below, I see plain text on the page, however the return Content-Type is still text/html.

How can I fix this?

NOTE: I'm using Tiles with Spring MVC. The returned "" points to a tiles def that maps to a health.jsp which only contains the 1 line below.

UPDATE NOTE: I have no control over the Content-Type or Accept values in the HTTP Header request. I want my response to return text/plain no matter what kind of request comes in.


@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");
    return "";



Best Solution

It should work if you annotate your method additionally with @ResponseBody:

@RequestMapping(value = "/",
                method = RequestMethod.GET)
public String plaintext(HttpServletResponse response) {
    return "TEXT";