/**
* 統一異常處理
* @return
*/
@RequestMapping("/exception")
public String exception(Date date) {
HelloModel helloModel = null;
helloModel.toString();
return "";
}
@ExceptionHandler(value = RuntimeException.class)
public Map exceptionHandller(){
Map handler = new HashMap();
handler.put("code","500");
handler.put("message","系統異常!");
return handler;
}複製代碼
說明:java
2、所有controller範圍內起做用的異常處理(全局異常處理)web
一、全局異常處理類spring
package com.wxx.demo.handler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @Author : leisure
* @Date : 2019/1/23
*/
//@ControllerAdvice(annotations = RestController.class)//指定註解類
//@ControllerAdvice(basePackages = {"com.demo.xx","com.demo.xx"})//指定掃描包
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 全局異常捕捉處理
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Map errorHandler(Exception ex) {
Map map = new HashMap();
map.put("code", 100);
map.put("msg", ex.getMessage());
return map;
}
}
複製代碼
說明:json
注意:bash