Skip to content
Advertisement

Pretty message on exceptions from REST app

I have a problem with my REST app. Here is the git link to ease your burden: https://github.com/TheNiceGuy123/AstonishingBackEnd

Here is the raw code also:

Classes posted in order:

Employee Controller:

JavaScript

Employee Service:

JavaScript

Global Exception Handler:

JavaScript

CNPNotFoundException class:

JavaScript

ApiErrorModel class:

JavaScript

I want to give some relevant informations to the user like in the second picture but in postman everything remains at the default level to call it like that, like my code from the handler is not even there. Is any logic or annotation that I miss?

This is how it looks on my side

This is how it should look like

Thank you for your time!

Advertisement

Answer

Problem is when a class is annotated with the @SpringBootApplication without any attribute SpringBoot only scans the package and its subpackages where the class itself is located.

In your case it means SpringBoot only looking for any Spring components under com.example.csmartbackend package while you put GlobalExceptionHandler class in exception.requestsexceptions package.

To fix it you have two choices:

  • put the exception package under com.example.csmartbackend, so it would be com.example.csmartbackend.exception.requestsexception.GlobalExceptionHandler
  • tell spring to scan the exception package as well, either by using scanBasePackageClasses or scanBasePackages attribute, e.g @SpringBootApplication(scanBasePackageClasses = {CSmartBackEndApplication.class, GlobalExceptionHandler.class})
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement