[英]How to display error message into a Json using ResponseStatusException on Spring boot?
在觸發錯誤請求時,我遇到了顯示錯誤消息的問題。
@DeleteMapping("/{projectId}/bugs/{bugId}")
public void deleteBug(@PathVariable (value = "projectId") Long projectId,
@PathVariable (value = "bugId") Long bugId){
if (!projectService.existById(projectId) || !bugService.existById(bugId)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "ProjectId " + projectId + " or BugId " + bugId + " not found");
}
bugService.deleteBug(bugId);
}
這是我觸發響應時的 JSON 響應:
{
"timestamp": "2020-05-29T15:40:41.302+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/projects/3/bugs/2" }
如您所見,該消息未出現。 如果我在代碼中更改 HttpStatus 它實際上可以工作,但由於某種原因,該消息不起作用。
我檢查了 class 的構造函數,它實際上只允許狀態和原因。
我錯過了什么還是 ResponseStatusException class 中的錯誤?
我也遇到過這種情況。 這不是錯誤,因此降級不是最佳解決方案。 Spring Boot 2.3.0 發行說明解釋:
更改默認錯誤頁面的內容
默認情況下,錯誤消息和任何綁定錯誤不再包含在默認錯誤頁面中。 這降低了向客戶泄露信息的風險。
server.error.include-message
和server.error.include-binding-errors
可用於分別控制消息的包含和綁定錯誤。 支持的值是always
、on-param
和never
。
因此,例如,在您的應用程序配置中,您可以將這些設置為顯示message
但不trace
output(YAML 示例):
server:
error:
include-message: ALWAYS
include-stacktrace: NEVER
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.