Return only string message from Spring MVC 3 Controller

后端 未结 7 703
死守一世寂寞
死守一世寂寞 2020-11-28 04:50

Can any one tell me how I can return string message from controller?

If i just return a string from a controller method then spring mvc treating it as a jsp view nam

7条回答
  •  余生分开走
    2020-11-28 05:02

    Although, @Tomasz is absolutely right there is another way:

    @RequestMapping(value="/controller", method=GET)
    public void foo(HttpServletResponse res) {
        try {       
            PrintWriter out = res.getWriter();
            out.println("Hello, world!");
            out.close();
        } catch (IOException ex) { 
            ...
        }
    }
    

    but the first method is preferable. You can use this method if you want to return response with custom content type or return binary type (file, etc...);

提交回复
热议问题