Spring Boot REST Service is not displaying the @ResponseStatus exception reason in JSON output -


I throw an exception to @ResponseStatus and JSON content in the foreground.

  @ResponseStatus (value = BAD_REQUEST, reason = 'exception.user.exists') class UserAlreadyExists Expanded RuntimeException {}  

If I try to resolve the message by then I get a local message.

Service Provides UserService UserService {@Autowired Private MessageSource messageSource private void verifyIsNewUser (user user) {If userRepository.countByEmail (user.email)} {DEF reason = this. MessageSource.getMessage ("exception.user.exists", tap, "just a message", localecontextholder .getLocale ()); Println Cause // User has already registered new userAdWordsExists ()}}

Question 1

Now if I debug, then I think message processing < Strong> null which is strange as implementing the ResponseStatusExceptionResolver interface MessageSourceAware should contain a message in Source Source Injection Why is it not?

Question 2

If I ignore the fact that I do not have any custom messages and look at the JSON output which is reaching the frontend, then I will face it with The result is: "bad", "Exception": "io.test.UserAlreadyExists", "Poor Request", "Error": 400, "Error", "Status": "1414403066197," "Timestamp" Message ": tap," path ":" / register / "}

there is nothing about the reason text field that I am using JSN There was hope to see the response. Something like this {..., "cause": "exception.user.exists", "message": null, ....} .

Why is the reason (key) in JSON response? When no message is received by a key, then seeing that code, instead of just using the key / message is used. Answer: A bug in Spring IMO ResponseStatusExceptionResolver a @

Bean is not , so it will not be injected with a MessageSource .

Answer 2: Requesting Spring Boot Boots If you are seeing default / error output from the boot, looking for a message in exception , not HttpServletResponse "Cause" code (I think that is available through a API). Here's a link.


Comments