eclipse 使用maven 构建springboot +全局异常与局部异常区别
来源:互联网 发布:网络节点是什么意思 编辑:程序博客网 时间:2024/06/08 09:36
一、controller 局部异常
package com.zzg.springbootone.controller;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.Api;@Api("测试controllerAdvice和全局异常处理")@RestController@RequestMapping("/advice1")public class AdviceController {@RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); } @ExceptionHandler(RuntimeException.class) public MyExceptionResponse exceptionHandler() { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode("300"); resp.setMsg("local exception-Handler"); return resp; }}
说明:
- 在controller中加入被@ExceptionHandler修饰的方法即可(在该注解中指定该方法需要处理的那些异常类)
- 该异常处理方法只在当前的controller中起作用
二、全部controller范围内起作用的异常处理(全局异常处理)
1、全局异常处理类
package com.zzg.springbootone.handler;import javax.servlet.http.HttpServletResponse;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import com.zzg.springbootone.controller.MyExceptionResponse;@ControllerAdvicepublic class GlobalExceptionHandler {@ExceptionHandler(RuntimeException.class) // @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class}) // @ExceptionHandler//处理所有异常 @ResponseBody //在返回自定义相应类的情况下必须有,这是@ControllerAdvice注解的规定 public MyExceptionResponse exceptionHandler(RuntimeException e, HttpServletResponse response) { MyExceptionResponse resp = new MyExceptionResponse(); resp.setCode("300"); resp.setMsg("global exception-Handler"); // response.setStatus(600); return resp; }}
说明:
- @ControllerAdvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类
- @ControllerAdvice可以指定扫描范围
- @ControllerAdvice约定了几种可行的返回值,如果是直接返回model类的话,需要使用@ResponseBody进行json转换
- 返回String,表示跳到某个view
- 返回modelAndView
- 返回model + @ResponseBody
package com.zzg.springbootone.controller;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.Api;@Api("测试controllerAdvice和全局异常处理")@RestController@RequestMapping("/advice1")public class AdviceController {@RequestMapping(value = "/test1", method = RequestMethod.GET) public String test1() { throw new RuntimeException("advice1 - exception1"); } @RequestMapping(value = "/test2", method = RequestMethod.GET) public String test2() { throw new RuntimeException("advice1 - exception2"); }// @ExceptionHandler(RuntimeException.class)// public MyExceptionResponse exceptionHandler() {// MyExceptionResponse resp = new MyExceptionResponse();// resp.setCode("300");// resp.setMsg("local exception-Handler");// return resp;// }}
注意:
- 同一个异常被局部范围异常处理器和全局范围异常处理器同时覆盖,会选择小范围的局部范围处理器
- 同一个异常被小范围的异常类和大范围的异常处理器同时覆盖,会选择小范围的异常处理器
参考自:
- http://jinnianshilongnian.iteye.com/blog/1866350 开涛的@ControllerAdvice(三个作用)
- http://www.tuicool.com/articles/fA7nuii springboot约定的异常处理体系
- https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc springMVC异常处理体系
- http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/ springMVC异常处理体系
package com.zzg.springbootone.controller;import java.io.Serializable;import lombok.Builder;import lombok.Data;@Builder@Datapublic class MyExceptionResponse implements Serializable {/** * */private static final long serialVersionUID = 1L;private String msg;private String code;public MyExceptionResponse(){}public MyExceptionResponse(String msg, String code) {super();this.msg = msg;this.code = code;}}
0 0
- eclipse 使用maven 构建springboot +全局异常与局部异常区别
- SpringBoot入门——局部与全局的异常处理
- SpringBoot全局异常处理
- Springboot 全局异常捕获
- springboot全局异常捕捉
- springboot全局异常捕获
- SpringBoot全局异常捕捉
- springboot 全局异常-ExceptionHandler
- springboot全局异常捕获
- springboot之全局异常
- springboot 全局异常捕捉
- eclipse 使用maven 构建springboot 程序
- eclipse 使用maven构建 springboot +swagger
- eclipse 使用maven 构建 springboot+mybatis
- eclipse 使用maven 构建springboot +log4j
- eclipse 使用maven 构建springboot +lombok
- eclipse 使用maven构建springboot+定时任务
- eclipse 使用maven 构建springboot+注入servlet
- C++ 标准模板库
- HDU-1004-Let the Balloon Rise
- 主线程和子线程均循环10次,两者交替运行50次
- 1016:部分A+B
- 深挖unity的资源管理
- eclipse 使用maven 构建springboot +全局异常与局部异常区别
- (升级版)arduino uno + dht11 + 舵机 +w5100网络模块 温度 网络控制舵机
- Android适配总结
- 集合(1)—List接口的实现类ArrayList
- 关于高斯模糊与opencv中的GaussianBlur函数
- Axure RP Extension for Chrome安装注意问题
- 上周总结
- jq获取checkbox状态以及有关触发checkbox的方式的讨论
- 打造H5里的“3D全景漫游”秘籍