/**
* 统一异常处理
* @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