spring mvc exception handling

Generally, when implementing a business, it is inevitable to throw some custom exceptions

Throw it to the controller for final processing. If the business is more complicated. Frequent try catch operations. 

Over time, code maintainability and readability will naturally come up.

Then, spring provides us with an annotation.

Personally understand that annotations are used lazily.

That's the @ControllerAdvice annotation

Paste the code directly: custom exception handling

Business code:

 

 The control layer does not handle the thrown exception. .

at this time. No need, you go to try cat sentence by sentence

There is no need for you to do too much configuration.

Official documentation:

 

access:

 

 

Validation annotation: @Valid

custom annotation

 

Change to a wrong phone number.

 

 

Personally, I prefer spring customization. That is, when you want to override the function, you only need to implement or inherit the corresponding interface and class

No need to deal with it too much. Just expand.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886803&siteId=291194637