当前位置:网站首页>24: Chapter 3: develop pass service: 7: user defined exceptions (to represent errors in the program); Create graceexceptionhandler to handle exceptions globally and uniformly (build JSON data of corre
24: Chapter 3: develop pass service: 7: user defined exceptions (to represent errors in the program); Create graceexceptionhandler to handle exceptions globally and uniformly (build JSON data of corre
2022-06-30 17:05:00 【Small withered forest】
explain :
【Spring Boot E-commerce projects 17: User module 6 : Registered interface development : Use 【GlobalExceptionHandler】 To handle exceptions globally ;】 User defined exceptions have been introduced , And the strategy of handling custom exceptions globally ; Before reading this blog , Be sure to refer to the above two blogs first ;
● What this blog does , The same as before ;; It's just , On the concrete realization , The mistake lies in the difference of yididididiu ;
stay 【23: The third chapter : Developing a pass service :6: Restrict users 60 Cannot repeat access within 【 Send a text message , Interface 】;】 in , In the interceptor , If it does “ The user is in 60 Repeat access within seconds 【 Send a text message , Interface 】” Of “ Wrong situation ” when , We just printed a sentence ;;; A formal practice , We can throw an exception , To represent this “ Wrong situation ”,
Catalog
1. stay 【imooc-news-dev-common】 In general engineering , establish exception package ;
2. Create custom exception :MyCustomException;( Inheritance is RuntimeException)
One : Formal development ;
1. stay 【imooc-news-dev-common】 In general engineering , establish exception package ;
About exception related classes , We can put it in this bag ;
2. Create custom exception :MyCustomException;( Inheritance is RuntimeException)
package com.imooc.exception; import com.imooc.grace.result.ResponseStatusEnum; /** * Custom exception ; * Purpose : Handle exception information uniformly ; Easy to manage , Easy to decouple ; */ public class MyCustomException extends RuntimeException { // Enumerating class objects private ResponseStatusEnum responseStatusEnum; /** * No arguments structure ; It's not what it takes. ; * It's just , Because we declare a parameterized construct , Its nonparametric construction automatically GG 了 ; * In order to prevent , There may be unnecessary trouble in the future , So I added a nonparametric structure to it ; */ public MyCustomException() { } /** * There are parametric structures : according to 【 incoming , Representing an error message ResponseStatusEnum Enumerating class objects 】, Sure * Instantiate a 【MyCustomException Exception object 】 * @param responseStatusEnum */ public MyCustomException(ResponseStatusEnum responseStatusEnum) { super(" The exception status code is :" + responseStatusEnum.status() + "; The specific information of the exception is :" + responseStatusEnum.msg()); this.responseStatusEnum = responseStatusEnum; } public ResponseStatusEnum getResponseStatusEnum() { return responseStatusEnum; } public void setResponseStatusEnum(ResponseStatusEnum responseStatusEnum) { this.responseStatusEnum = responseStatusEnum; } }explain :
(1) Our custom exception , Inheritance is RuntimeException;; and , We are 【Spring Boot E-commerce projects 16: User module 5 : Registered interface development : Custom exception classes ;】 Custom exception in , Inheritance is Exception;
● adopt 【Java Exception one : Exception definition ; Classification of exceptions 】 You know ,RuntimeException Yes no check abnormal ;;; therefore , When we throw a in the program RuntimeException Abnormal times , We don't need to do it try-catch;;;; thus , We don't know much about code “ Invasive ”;
(2) It is known that , We are in the enumeration class , A lot of error messages are defined ;;;;; When something goes wrong with the program , Sometimes we can use the corresponding error messages , Build a custom MyCustomException Exception object ;
3. Create a tool class :GraceException: According to what was passed in 【ResponseStatusEnum Enumeration class 】, establish ( And throw ) Corresponding customization MyCustomException Exception object ;
package com.imooc.exception; import com.imooc.grace.result.ResponseStatusEnum; /** * Tool class : according to 【 incoming , Representing an error message ResponseStatusEnum Enumerating class objects 】, system * A package 【 Our custom exception MyCustomException abnormal , The object of 】 */ public class GraceException { /** * According to what was passed in 【ResponseStatusEnum Enumeration class , * Create and throw our custom exception MyCustomException object 】 * @param responseStatusEnum */ public static void display(ResponseStatusEnum responseStatusEnum) { throw new MyCustomException(responseStatusEnum); } }explain :
(1) Class content description ;
above , When we throw a custom exception in the program , This custom exception is thrown , Need to be captured by the container ;;; After the container catches this custom exception , We can put the corresponding specific error information , Throw to the front ;
therefore , At this point, we need to do a unified interception ; This is what follows ;
4. establish “ Handle exceptions uniformly handler class ”:GraceExceptionHandler class : To handle exceptions globally , According to 【 Abnormal information 】 Convert to corresponding 【API Unified return object 】;
package com.imooc.exception; import com.imooc.grace.result.GraceJSONResult; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * Unified exception interception and handling ; * Specific information about exceptions can be provided , Build the corresponding GraceJSONResultAPI Unified return object , To the front end ; */ @ControllerAdvice public class GraceExceptionHandler { @ExceptionHandler(MyCustomException.class) @ResponseBody public GraceJSONResult returnMyException(MyCustomException e) { e.printStackTrace(); return GraceJSONResult.exception(e.getResponseStatusEnum()); } }explain :
(1) We are 【Spring Boot E-commerce projects 17: User module 6 : Registered interface development : Use 【GlobalExceptionHandler】 To handle exceptions globally ;】 in , Have done similar work ;;;; The content here , In this blog , There are introductions ;
(2) Take a look 【@ControllerAdvice】 annotation , In fact, you can understand , Here too AOP Principle , To achieve the ;
(3) here , It's based on 【 Abnormal information 】 Convert to corresponding 【API Unified return object 】;;; We are 【14: Chapter two : Architecture back end project :10: encapsulation “ Return results ”;】 in , Created 【API Unified return object 】 namely GraceJSONResult class ;
(4) Self perception ( Understanding may be biased or even wrong ):
● Statement 1: The response is coming soon , That is, when you want to return to the front-end content (PS: This joint is the eye , Is not necessarily Controller Oh , It could also be an interceptor ;; namely , That returns information directly to the front end , Is not necessarily Controller, It may also be interceptors, etc ), It is found that there is another MyCustomException abnormal ;;;;; that , We created here GraceExceptionHandler class , It USES 【 @ExceptionHandler(MyCustomException.class)】 The method of annotation , Will capture , According to 【 Abnormal information 】 Convert to corresponding 【API Unified return object 】 Of JSON Formatted data , Back to the front end ;
● Statement 2: This GraceExceptionHandler class , Real time monitoring 【 The point immediately to return to the front-end content 】( here ,Controller And interceptors , It can be regarded as equivalent );; If , Its have MyCustomException abnormal ;;; that , We created here GraceExceptionHandler class , It USES 【 @ExceptionHandler(MyCustomException.class)】 The method of annotation , Will capture , According to 【 Abnormal information 】 Convert to corresponding 【API Unified return object 】 Of JSON Formatted data , Back to the front end ;
Two : then , stay 【23: The third chapter : Developing a pass service :6: Restrict users 60 Cannot repeat access within 【 Send a text message , Interface 】;】 in , In the interceptor , If it does “ The user is in 60 Repeat access within seconds 【 Send a text message , Interface 】” Of “ Wrong situation ” when , You can throw a corresponding exception ;
3、 ... and : effect ;
(1) First, the whole situation install Look at the whole project ;
(2) then , start-up 【user】 The main startup class of ;
(3) then , On the front page , Two consecutive visits 【 Send SMS interface 】;(PS: Remember to turn on SwitchHosts Virtual domain name )
边栏推荐
- More dragon lizard self-developed features! Production available Anolis OS 8.6 officially released
- Raft introduction
- Mathematical modeling for war preparation 36 time series model 2
- Cmakelists Basics
- [wechat applet] the hosting environment of the applet
- [wechat applet] basic use of common components (view/scroll-view/wiper, text/rich-text, button/image)
- IO stream_ recursion
- Additional: (not written yet, don't look at ~ ~ ~) webmvcconfigurer interface;
- Eight basic sorting (detailed explanation)
- Mathematical modeling for war preparation 34-bp neural network prediction 2
猜你喜欢

山西化工园区智能化管控平台建设时间表

IO stream_ recursion

Etcd教程 — 第八章 Etcd之Compact、Watch和Lease API

More dragon lizard self-developed features! Production available Anolis OS 8.6 officially released

OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义

Differential analysis between different groups nichenet for silicosis runs successfully!

Construction schedule of intelligent management and control platform in Shanxi Chemical Industry Park
![[wechat applet] basic use of common components (view/scroll-view/wiper, text/rich-text, button/image)](/img/3b/05dbf03024088c5f94363f157a1701.png)
[wechat applet] basic use of common components (view/scroll-view/wiper, text/rich-text, button/image)

Restartprocessifvisible process

Raft介绍
随机推荐
Undistorted resize using pil
addmodule_allmerge_ams_im
Compile - compile for itop4412 development board makefile
Php7.3 centos7.9 installing sqlserver extensions
flutter自定义组件
Jsr303 and common validator implementations
Etcd tutorial - Chapter 8 compact, watch, and lease APIs for etcd
TCP socket and TCP connection
[demo] write file circularly
为了使远程工作不受影响,我写了一个内部的聊天室 | 社区征文
If your MES is not upgraded, it will be eliminated
Niuke.com: minimum cost of climbing stairs
POJ Project Summer
登录框Tricks
Deep learning - (2) several common loss functions
OpenCV中LineTypes各枚举值(LINE_4 、LINE_8 、LINE_AA )的含义
Nichenet actual silicosis
Cmakelists Basics
restartProcessIfVisible的流程
STL tutorial 7-set, pair pair pair group and functor











