zoukankan      html  css  js  c++  java
  • springboot编程之全局异常捕获

    springboot编程之全局异常捕获

    1、创建GlobalExceptionHandler.java,在类上注解@ControllerAdvice,

    在方法上注解@ExceptionHandler(value = Exception.class),Exception.class表示拦截所有的异常信息

    package com.imooc.web.controller;
    
    import com.imooc.exception.UserNotExistException;
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.ResponseStatus;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @ControllerAdvice
    public class GlobalExceptionHandler {
    
        @ExceptionHandler(Exception.class)
        @ResponseBody
        @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
        public Map<String, Object> handleUserNotExistException(Exception ex) {
            Map<String, Object> result = new HashMap<>();
            result.put("message", ex.getMessage());
            return result;
        }
        
        
        
        
    }
  • 相关阅读:
    openssh升级
    Mysql基础学习_Windows版(一)
    centos7 取消Ctrl+Alt+Del重启功能
    linux shell数值比较和字符串比较
    linux 使用中括号进行条件判断
    VXLAN简介(摘抄)
    centos配置NTP服务器
    Centos配置网卡子接口
    组播___IGMP
    组播
  • 原文地址:https://www.cnblogs.com/dw3306/p/10925338.html
Copyright © 2011-2022 走看看