我想回到我的Rest客户最简单的答案。只有:
最简单的方法是什么?
我曾经用这种方式使用ResponseEntity
对象:
return new ResponseEntity<String>("Custom string answer", HttpStatus.CREATED);
,
但是不幸的是,我不能简单地在构造函数中传递http头。
我必须创建HttpHeaders
对象,并在那里添加自定义标题,如下所示:
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE);
return new ResponseEntity<String>("Custom string answer", headers, HttpStatus.CREATED);
但我在找更简单的东西。可以符合一行代码的东西。
有人能帮忙吗?
发布于 2016-06-07 13:25:06
正如@M.Deinum已经提出的,这是最简单的方法:
@RequestMapping("someMapping")
@ResponseBody
public ResponseEntity<String> create() {
return ResponseEntity.status(HttpStatus.CREATED)
.contentType(MediaType.TEXT_PLAIN)
.body("Custom string answer");
}
发布于 2016-06-07 07:37:58
我想这会有帮助:
@RequestMapping(value = "/createData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public String create(@RequestBody Object input)
{
return "custom string";
}
https://stackoverflow.com/questions/37677600
复制相似问题