Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee My腾云code

Happy_EveryDay

可以平凡 不可以平庸 无爱则无忧,无欲则无求,无怒而无敌,无怨才是佛。所有烦恼,都是放不下的执著 开源技群 328035181 MyGitee

springboot + 全局异常处理

ControllerAdvice

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;
@ControllerAdvice
public class MayiktExceptionHandler {

  @ExceptionHandler({RuntimeException.class})
  @ResponseBody
  public Map<String,String> exceptionHandler(){
    HashMap<String,String > objectObjectHashMap=new HashMap<>();
    objectObjectHashMap.put("respCode","500");
    objectObjectHashMap.put("respMsg","系统错识");
    return objectObjectHashMap;
  }
}

 

 


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

  @RestController
  public class TestContoller {

    @RequestMapping("/test1")
    public String test1(String username,Integer age){
      int j=1/age;
      return "error";
    }

}

posted on 2020-09-11 10:15  cn2025  阅读(179)  评论(0)    收藏  举报

导航