https://github.com/spring-projects/spring-boot/blob/b3278f459e9f69bac948e691d07d79330da7ff20/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/error/DefaultErrorAttributes.java#L121

https://github.com/spring-projects/spring-boot/blob/b3278f459e9f69bac948e691d07d79330da7ff20/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/reactive/error/DefaultErrorAttributes.java#L98

I carefully checked the DefaultErrorAttributes.java code context, I think the judgment statement errorAttributes.get("message") != null will always be true, and even if it is not true, this judgment will not affect the execution of errorAttributes.put("message", "");

So I think it is not necessary to have a conditional judgment

I understand the source code is errorAttributes.put("message", ""); instead of errorAttributes.remove("message"); because no matter whether server.error.include-message is always or never, the response body should always be Contains the message field, right? If it is, it should be indicated in the configuration file to distinguish it from other server.error.*, because the current explanation says When to include "message" attribute. It is easy for users to misunderstand.

Comment From: pivotal-issuemaster

@weixsun Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-issuemaster

@weixsun Thank you for signing the Contributor License Agreement!

Comment From: philwebb

I can't remember why we add an empty message rather than removing the entry. It might have been for back-compatibility. I wonder if we should change things in 2.5.x so that the entry is removed entirely.

Comment From: scottfrederick

I also suspect that the message attribute is handled differently from the other attributes for back-compatibility reasons, although it does not appear we have tests that verify the existing behavior. If we make a change like the one suggested I agree it should be in a minor release not a patch.

Comment From: weixsun

I can't remember why we add an empty message rather than removing the entry. It might have been for back-compatibility. I wonder if we should change things in 2.5.x so that the entry is removed entirely.

Add empty message from here : https://github.com/spring-projects/spring-boot/issues/20505#issuecomment-621295137

Comment From: philwebb

We discussed this today as a team and decided to try to remove the error attribute rather than blank it. We'll handle that as an enhancement (#24712, hopefully in 2.5).

Looking at the current PR, I think it's possible that a user may have overridden the DefaultErrorAttributes.getErrorAttributes that returns the Map and may also have removed the message entry. As such, I don't think it's safe to assume that the judgment statement can be removed.

Thanks for the PR and for prompting the discussion.