我有以下端點:
@ApiResponses(value = {
@ApiResponse(responseCode = "204", content = {@Content(schema = @Schema(implementation = UserDTO.class))}, description = "Successful")
})
@DeleteMapping("/Users/{id}")
public ResponseEntity<String> deleteUser(@PathVariable("id") String id) {
userService.deleteUser(id);
return ResponseEntity.noContent().header("Successfully deleted", "0").build();
}
我需要自定義 204 回應 - 不僅回傳狀態和空主體,還回傳 204 代碼 將包含“已成功洗掉”訊息的主體。我試圖手動添加標題(在上面的代碼中),但它沒有正常作業。你能給我一些建議嗎 - 我怎樣才能用不同的方式添加它?
uj5u.com熱心網友回復:
正如馬克在他的評論中正確指出的那樣,204同時回傳和身體是沒有意義的,因為那不是什么204意思。我建議您只需回傳以下內容:
return new ResponseEntity<>("Successfully deleted", HttpStatus.OK);
對于您所描述的內容,您實際上并不需要任何自定義標頭,但如果您仍然覺得需要一個,則可以這樣做
HttpHeaders headers = new HttpHeaders();
headers.add("custom-header", "value");
return new ResponseEntity<>("Successfully deleted", headers, HttpStatus.OK);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533624.html
