服务端异常讲解和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"}
返回页面:
附上目录结构:
|