且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅从 Spring MVC 3 控制器返回字符串消息

更新时间:2023-02-12 23:18:07

@ResponseBody 在控制器中注释你的方法:

Annotate your method in controller with @ResponseBody:

@RequestMapping(value="/controller", method=GET)
@ResponseBody
public String foo() {
    return "Response!";
}

来自:15.3.2.6 使用@ResponseBody注解映射响应体:

From: 15.3.2.6 Mapping the response body with the @ResponseBody annotation:

@ResponseBody 注释 [...] 可以放在方法上,指示返回类型应直接写入 HTTP 响应正文(而不是放在模型中,或解释作为视图名称).

The @ResponseBody annotation [...] can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name).