@ControllerAdvice
public class GloExceotion {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Result runtimeException(){
Result result = new Result(200,"错误",new Object());
return result;
}
@ExceptionHandler(FileNotFoundException.class)
public ModelAndView fileNotException(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/erro2.html");
return modelAndView;
}
@ExceptionHandler(Exception.class)
public ModelAndView Exception(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/default.html");
return modelAndView;
}
}
例子
java复制代码
@RestController
public class ExceptionController {
@RequestMapping("/e1")
public String e1 (){
int a=10/0;
return "ruuning exception";
}
@RequestMapping("/e2")
public String e2() throws FileNotFoundException {
FileInputStream fileInputStream = new FileInputStream("file:/barch:/");
return "ruuning exception";
}
@RequestMapping("/e3")
public String e3() {
int [] array ={1,2};
System.out.println(array[5]);
return "ruuning exception";
}
}