简体   繁体   中英

“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"
}

I tried using,

throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid credentials");

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 version: 2.3.0 Java Version: 11.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

All the default values are listed here: Spring Boot Reference Documentation

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM