SpringBoot logic abnormal unified treatment

Building project

We abnormal extracted core logic processing section as a separate jarsupply of other modules references, create a project parentitem pom.xmladded to rely on public use, the configuration shown as follows:

<dependencies>
        <!--Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--测试模块依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

 

After the project is created in addition to .idea, iml, pom.xmlretention, others are deleted.

Exception handling core sub-modules

/ ** 
 * @author the WGR 
 * @Create 2019/9/7 - 15:06 
 * / 
public  class OssException the extends a RuntimeException the implements the Serializable { 
    Private  static  Final  Long serialVersionUID = 1L ; 
    Private Object [] errFormatArr; 
    public OssException ( Message String, ... Object obj) {
         Super (Message);
         the this .errFormatArr = obj; 
    } 
    // for practical needs, so added an additional two constructors 
    public OssException(String message, Throwable cause) {
        super(message, cause);
    }
​
    public OssException(Throwable cause) {
        super(cause);
    }
​
    public Object[] getErrFormatArr() {
        return errFormatArr;
    }
​
    public void setErrFormatArr(Object[] errFormatArr) {
        this.errFormatArr = errFormatArr;
    }
}

 

Uniform definitions return results

SLF4J @ 
@ControllerAdvice 
public  class OssExceptionHandler { 
    @ExceptionHandler (value = Exception. Class ) 
    @ResponseBody 
    public ModelAndView handle (Exception EX) {
         // use FastJsonJsonView FastJson view provided to return, no catch exceptions 
        FastJsonJsonView = View new new FastJsonJsonView (); 
        Result R & lt = null ;
         IF (EX the instanceof OssException) { // from abnormal sense 
            Result = M.getErrR (ex.getMessage (), ((OssException) EX) .getErrFormatArr ());
        } The else  IF (EX the instanceof MaxUploadSizeExceededException) { // the Spring file upload size abnormality 
            Result = M.getErrR ( "exception.maxUploadSizeExceededException", PropUtil.getInteger ( "upload.maxSize" )); 
        } the else  IF (EX the instanceof the DataAccessException) { // the Spring of JDBC exceptions 
            Result = M.getErrR ( "exception.dataAccessException" ); 
        } the else { // other unknown abnormalities 
            Result = M.keyErrR ( "exception.other" ); 
        } 
        // development process printing at the exception information, the production process can be shut down 
        if (result.getErrCode ()! = 60113) { // 20,181,225 login session failure, not printed 
            String the stackTrace = StackUtil.getStackTrace (EX); 
            log.error ( "----->" + the stackTrace); 
        } 
        // the computer side, the abnormality information package 20181128 security test question requires close detailed abnormality information
         // IF (WebUtil.isComputer ()) result.setErrdetail (the stackTrace); 
        result.setErrdetail (ex.getMessage ()); // 20,190,128 abnormality information simple need to join 
        view.setAttributesMap (Result); 
        return  new new ModelAndView (View); 
    } 
}

 

For various reasons, we can only put part of the code, can provide ideas.

Guess you like

Origin www.cnblogs.com/dalianpai/p/11756148.html