[英]Return HTTP 204 on null with spring @RestController
[英]Why Spring RestController POST method returns 200 instead of 204 when return type is void?
@PostMapping(path = "/test")
public void save(Map payload) {
testService.save(payload);
}
Spring RestController 在上述情況下返回 204 而不是 200。 返回類型為無效且沒有內容是響應正文。 在沒有響應體且處理成功的情況下,返回 204 HTTP 碼是慣例。 我想知道為什么 Spring 默認不這樣做? 有什么特別的原因嗎?
我已經知道如何更改默認狀態碼,所以這不是問題。
試試這個代碼
@PostMapping(path = "/saveAccident")
ResponseEntity<AccidentDTO> saveAccident(@RequestBody AccidentDTO accidentDTO) {
return new ResponseEntity<AccidentDTO>(accidentService.saveAccident(accidentDTO), HttpStatus.OK);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.