我正在尝试使用新的Spring 4.0 @RestController从控制器返回简单的文本响应:
@RestController
@RequestMapping(value = "/heartbeat")
public class HeartbeatController {
private static final Logger logger = LoggerFactory.getLogger(HeartbeatController.class);
@RequestMapping
public String heartbeat() {
logger.info("Received heartbeat!");
return "I'm Alive!";
}
@RequestMapping(value = "/test", produces = MediaType.TEXT_PLAIN_VALUE)
public String heartbeat2() {
logger.info("Received heartbeat!");
return "I'm Alive!";
}
}
当我访问/heartbeat时,我得到以下响应:
"I'm Alive!"
结果中包含双引号,这是我没有预料到的。
当我访问/heartbeat/test时,我得到一个空响应,但我期望看到“I'm Alive!”文本。
更新
curl -i http://myserver.com/rest/heartbeat
HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Server: Development/1.0 Date: Tue, 17 Dec 2013 18:59:08 GMT Cache-Control: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 12
"I'm Alive!"
curl -i -H "Accept: application/json" http://myserver.com/rest/heartbeat HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Server: Development/1.0 Date: Tue, 17 Dec 2013 19:01:12 GMT Cache-Control: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 12
"I'm Alive!"
curl -i http://myserver.com/rest/heartbeat/test
HTTP/1.1 406 Not Acceptable Server: Development/1.0 Date: Tue, 17 Dec 2013 19:00:13 GMT Cache-Control: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 0
curl -i -H "Accept: text/plain" http://myserver.com/rest/heartbeat/test
HTTP/1.1 406 Not Acceptable Server: Development/1.0 Date: Tue, 17 Dec 2013 19:02:06 GMT Cache-Control: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT Content-Length: 0
@ResponseBody
了吗?我只是好奇。 - marco.eig