服務端異常講解和SpringBoot配置全局異常實戰
注解介紹:
@ControllerAdvice 如果是返回json數據,則用RestControllerAdvice,就可以不加@ResponseBody
捕獲全局異常,處理所有不可知的異常
@ExceptionHandler(value=Exception.class)
Controller代碼:
package net.nbclass.demo.controller; import net.nbclass.demo.Module.ExecptionData; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 異常測試 * Created by 5407 on 2018/9/25. */ @RestController public class ExecptionController { @RequestMapping("/ext") public String test(){ int i=1/0; --異常觸發 return "Hello"; } }
異常處理類:
package net.nbclass.demo.Handler; import net.nbclass.demo.Module.ExecptionData; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * Created by 5407 on 2018/9/25. */ @RestControllerAdvice public class CustomExtHandler { @ExceptionHandler(value = Exception.class) Object handleException(Exception e, HttpServletRequest request){ Map<String,Object> map=new HashMap<>(); map.put("code",100); map.put("msg",e.getMessage()); map.put("url",request.getRequestURL()); return map; } }
服務端異常講解和SpringBoot配置自定義異常實戰
和全局異常區別不大,只是多了一點:
新建自定義異常類,需要繼承基類【RuntimeException】:
package net.nbclass.demo.Module; /** * 自定義異常類 * Created by 5407 on 2018/9/25. */ public class ExecptionData extends RuntimeException{ public ExecptionData(String code,String msg){ this.code=code; this.msg=msg; } private String code; private String msg; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
捕獲異常並處理:
package net.nbclass.demo.Handler; import net.nbclass.demo.Module.ExecptionData; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * Created by 5407 on 2018/9/25. */ @RestControllerAdvice public class CustomExtHandler { @ExceptionHandler(value = Exception.class) Object handleException(Exception e, HttpServletRequest request){ Map<String,Object> map=new HashMap<>(); map.put("code",100); map.put("msg",e.getMessage()); map.put("url",request.getRequestURL()); return map; } @ExceptionHandler(value = ExecptionData.class) Object ExecptionData(ExecptionData e, HttpServletRequest request){
Map<String,Object> map=new HashMap<>(); map.put("code",11); map.put("msg",e.getMessage()); map.put("url",request.getRequestURL()); return map;
//結合thymeleaf模板引擎實現頁面跳轉
//ModelAndView modelAndView = new ModelAndView();
//modelAndView.setViewName("index");
//modelAndView.addObject("msg", e.getMessage());
//return modelAndView;
}
}
Controller類觸發異常:
package net.nbclass.demo.controller; import net.nbclass.demo.Module.ExecptionData; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 異常測試 * Created by 5407 on 2018/9/25. */ @RestController public class ExecptionController { @RequestMapping("/ext") public String test(){ int i=1/0; return "Hello"; } @RequestMapping("/ext1") public Object test1(){ throw new ExecptionData("101","error"); } }
結果:
{"msg":null,"code":11,"url":"http://localhost:8080/ext1"}
返回頁面:
附上目錄結構: