自定义异常,在平常项目开发中用处很大html
一、先建立一个异常统一处理类 java
package com.black.example.helloworld.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * 异常统一处理方法 */ @ControllerAdvice public class MyHandlerAdvice { /** * 全局捕获异常,只要做用在@RequestMapping方法上,全部的信息都会被捕捉到 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = Exception.class) public Map<String,Object> errorHandler(Exception ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",-1); map.put("msg",ex.getMessage()); return map; } }
二、假定在HelloWorldHandler中的index方法上,设置一些异常代码,以下图:web
@RequestMapping(value = "/index.html") public String index(){ int code = 1/0; return "Hello Black!!!!!"; }
三、重启访问 http://localhost:8090/black/index.htmlspring
一、自定义异常实体类app
package com.black.example.helloworld.exception; /** * 自定义异常 */ public class BusinessException extends RuntimeException { private String code; private String msg; public BusinessException(String code,String msg){ this.code = code; this.msg = 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; } }
二、自定义异常方法this
package com.black.example.helloworld.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * 异常统一处理方法 */ @ControllerAdvice public class MyHandlerAdvice { /** * 全局捕获异常,只要做用在@RequestMapping方法上,全部的信息都会被捕捉到 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = Exception.class) public Map<String,Object> errorHandler(Exception ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",-1); map.put("msg",ex.getMessage()); return map; } /** * 自定义异常 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = BusinessException.class) public Map<String,Object> errorBussinessHandler(BusinessException ex){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code",ex.getCode()); map.put("msg",ex.getMsg()); return map; } }
三、假定程序抛出异常spa
package com.black.example.helloworld.web; import com.black.example.helloworld.exception.BusinessException; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by 10250H on 2018/5/15. */ @RestController public class HelloWorldHandler { //获取application.properties自定义配置的值 @Value("${black.msg}") private String msg; @RequestMapping(value = "/index.html") public String index(){ /*int code = 1/0; return "Hello Black!!!!!";*/ throw new BusinessException("500","用户名或者密码错误"); } @RequestMapping(value = "/customConfig.html") public String customConfig(){ return this.msg; } }
四、重启服务,观察结果code