zoukankan      html  css  js  c++  java
  • [spring-boot] ControllerAdvice异常类

    package com.example.demo.exception;
    
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Anny的springboot項目異常類
     */
    @ControllerAdvice
    public class MyControllerAdvice {
    
        /**
         * 全局捕获异常类,
         *      · 只作用在RequestMapping方法上,所有的异常信息都会被捕获
         * @param ex
         * @return
         */
        @ResponseBody
        @ExceptionHandler(value = Exception.class)
        public Map<String,Object> errorHandler(Exception ex){
            Map<String,Object> map = new HashMap<>();
            map.put("code",-1);
            map.put("msg",ex.getMessage());
            return map;
        }
    
        /**
         * 自定义异常
         * @param ex
         * @return
         */
        @ResponseBody
        @ExceptionHandler(value = BusinessException.class)
        public Map<String,Object> errorBusinessHandler(BusinessException ex){
            Map<String,Object> map = new HashMap<>();
            map.put("code",ex.getCode());
            map.put("msg",ex.getMsg());
            return map;
        }
    
    }
    package com.example.demo.exception;
    
    /**
     * 自定义异常类
     */
    public class BusinessException extends RuntimeException {
    
        private int code;
        private String msg;
    
        public BusinessException(int code, String msg) {
            super();
            this.code = code;
            this.msg = msg;
        }
    
        public int getCode() {
            return code;
        }
    
        public void setCode(int code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    package com.example.demo.controller;
    
    import com.example.demo.exception.BusinessException;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    @RequestMapping("/hello")
    public class HelloController {
    
        private static Logger logger = LoggerFactory.getLogger(HelloController.class);
    
        @Value("${anny.msg}")
        private String msg;
    
        @RequestMapping("/springBoot")
        public String Hello(){
            //系统异常
    //        int no = 1/0;
    //        return msg;
            //自定义异常
            throw new BusinessException(999999999,"你瞅啥");
        }
    }
  • 相关阅读:
    快速实现进度条
    Cristi Potlog's Chart Control for .NET
    摩托罗拉投资Android外来往戏开辟商Moblyng
    展讯通讯四季度净利润3000万美元同比增长20倍
    罗仕证券上调空中网评级至"买入"
    政协委员郭为:立异体系体例要以运用为先
    软通动力第四季度净盈余390万美元
    华盛顿邮报:iPad 2将扩展抢先职位
    TwitPic借绯闻男星营销 每个单词赚1.6美元
    苹果向2周内新购iPad一代用户退款100美元
  • 原文地址:https://www.cnblogs.com/anny0404/p/11022925.html
Copyright © 2011-2022 走看看