如何使用Spring Boot中的ResponseStatusException将错误消息显示为Json?

huangapple go评论67阅读模式
英文:

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,实际上是可以工作的,但由于某种原因,消息无法正常工作。

我已经检查了该类的构造函数,它只允许设置状态和原因。

我是否漏掉了什么,还是ResponseStatusException 类中存在 bug?

英文:

I have an issue displaying an error message at the moment that a bad request is triggered.

@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);
}

This is the JSON response when I trigger the Response:

{
"timestamp": "2020-05-29T15:40:41.302+00:00",
"status": 400,
"error": "Bad Request",
"message": "",
"path": "/projects/3/bugs/2"  }

As you can see the message is not appearing. If I change the HttpStatus in the code it actually works but for some reason, the message is not working.

I checked the constructor of the class and it actually allows only the status and the reason.

Am I missing something or is it a bug in the ResponseStatusException class?

答案1

得分: 9

我也遇到了这个问题。这不是一个错误,所以降级不是最好的解决方法。Spring Boot 2.3.0 发布说明 解释道:

默认错误页面内容的变更

错误消息和任何绑定错误不再默认包含在默认错误页面中。这降低了向客户端泄露信息的风险。server.error.include-messageserver.error.include-binding-errors 可用于控制是否包含消息和绑定错误。支持的值包括 alwayson-paramnever

因此,例如,在应用程序配置中,您可以设置这些来显示 message 但不显示 trace 输出(YAML 示例):

server:
  error:
    include-message: ALWAYS
    include-stacktrace: NEVER
英文:

I also encountered this. It's not a bug, so downgrading is not the best resolution. The Spring Boot 2.3.0 release notes explain:

> ### Changes to the Default Error Page's Content
> The error message and any binding errors are no longer included in the
> default error page by default. This reduces the risk of leaking
> information to a client. server.error.include-message and
> server.error.include-binding-errors can be used to control the
> inclusion of the message and binding errors respectively. Supported
> values are always, on-param, and never.

So for example, in your application configuration you can set these to show message but not trace output (YAML example):

server:
  error:
    include-message: ALWAYS
    include-stacktrace: NEVER

huangapple
  • 本文由 发表于 2020年5月29日 23:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/62089792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定