Possible Duplicate:
Return only string message from Spring MVC 3 Controller
我的 spring Controller 有一个我想要的端点:
1. set the http response code
2. return a string back, don't need to render a .jsp view page or anything.
所以需要将 http 状态码设置为 200 OK 或 500 等。 并简单地返回一个类似“OK”的字符串。
我该怎么做,还是我被迫渲染一个 .jsp View 页面?
最佳答案
使用 @ResponseBody
注释:
@RequestMapping(value="/sayHello", method=GET)
@ResponseBody
public String whatever() {
return "Hello";
}
见 @ResponseBody
ref docs了解更多详情。
您也许可以使用 @ResponseStatus
注释来设置代码,而不是直接使用 HttpServletResponse
。
关于java - 希望我的 Spring Controller 返回一个没有 View 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8701869/