zoukankan      html  css  js  c++  java
  • Spring Boot异常处理

    一.异常类
    package com.chx.springboot.exception;
     
     
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ResponseBody;
     
     
    import java.util.HashMap;
    import java.util.Map;
     
     
    @ControllerAdvice
    public class ExceptionHandler {
        //捕获运行时异常
        @org.springframework.web.bind.annotation.ExceptionHandler(RuntimeException.class)
        @ResponseBody
        public Map<String,Object> exceHandler() {
            Map<String, Object> map = new HashMap<>();
            map.put("error", "500");
            map.put("msg", "您好,服务器暂时出现异常,请稍后重试");
            return map;
        }
    }
     
    二.控制层模拟异常
    package com.chx.springboot.controller;
     
     
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
     
     
    @RestController
    @RequestMapping("/hello")
    public class MyController {
        @RequestMapping("/sayHello")
        public String sayHello(){
            //模拟运行时异常
            int result=5/0;
            return "hello springboot";
        }
    }
     
    三.运行结果
     
     
  • 相关阅读:
    spark-RDD缓存,checkpoint机制,有向无环图,stage
    spark广播变量
    k8s部署spark
    spark简单安装
    搭建一套高可用的hadoop集群
    JavaScript实现无限级递归树的示例代码
    $.ajax 调用 Asp.Net Core Razor Page 后台代码
    2020蓝桥杯省赛B组第二轮 H 字串分值
    取整函数
    c++按空格分割句子
  • 原文地址:https://www.cnblogs.com/chx9832/p/12017625.html
Copyright © 2011-2022 走看看