“消息"错误响应 Spring Boot 中的字段为空 [英] "message" field is empty in error response Spring Boot
问题描述
我处理过这样未经授权的异常
I have handled unauthorized exception like this
@ResponseStatus(value= HttpStatus.UNAUTHORIZED)
public class UnauthorizedException extends RuntimeException {
public UnauthorizedException(String message) {
super(message);
}
}
但是当我抛出如下错误时
but when I throw the error as follows
throw new UnauthorizedException("Invalid credentials");
我在响应中得到的错误消息为空
I get the error message empty in the response as follows
{
"timestamp": "2020-05-27T13:44:58.032+00:00",
"status": 401,
"error": "Unauthorized",
"message": "",
"path": "/auth/register"
}
我尝试使用,
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid credentials");
但结果还是一样.当我添加 spring-boot-devtools 依赖项时,我也按预期收到了带有堆栈跟踪的消息.我该如何解决这个问题?
but the result is still the same. When I added spring-boot-devtools dependency, I get the message as expected with the stack trace as well. How can I fix this?
Spring 版本:2.3.0Java 版本:11.0
Spring version : 2.3.0 Java Version: 11.0
推荐答案
好的,他们已经将 server.error.include-message 的默认值更改为never"(请参阅此处的评论部分:Spring 2.3.0 发布信息
Ok got it, they have changed the default value for server.error.include-message to "never" (see the comment section here: Spring 2.3.0 Release Info
此处列出了所有默认值:Spring Boot 参考文档
All the default values are listed here: Spring Boot Reference Documentation
只需像这样配置您的 application.yaml(或属性).
Just configure your application.yaml (or properties) like this.
server:
error:
include-message: always
include-binding-errors: always
请注意,显示错误消息可能会泄露有关您的服务器实现的信息.
Mind that showing error messages may leak information about your server implementation.
这篇关于“消息"错误响应 Spring Boot 中的字段为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!