spring mvc异常统一处理(ControllerAdvice注解)
2016-07-18 17:56
489 查看
首先我的项目是一个为移动端提供的json数据的,当后台报错时如果为移动端返回一个错误页面显得非常不友好,于是通过ControllerAdvice注解返回json数据。
首先创建一个异常处理类:
加上ControllerAdvice注解,注意这个类是在controller包下面,因为spring需要扫描到,
代码中的:
表示捕捉到所有的异常,你也可以捕捉一个你自定义的异常,比如:
然后我在一个接口中故意抛出一个异常:
最后后台返回的数据是:
首先创建一个异常处理类:
package com.gefufeng.controller; import com.gefufeng.common.exception.KnownBizException; import org.apache.commons.lang.StringUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.http.HttpStatus; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MissingServletRequestParameterException; 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.ResponseStatus; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * Created by gefufeng on 16/7/18. */ @ControllerAdvice public class ApplicationControllerExceptionHandler { private static final Logger LOGGER = LogManager.getLogger(ApplicationControllerExceptionHandler.class); @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody public Map<String, Object> handlerError(HttpServletRequest req, Exception e) { map.put("tip", "此错误说明调用接口失败,失败原因见msg,如果msg为空,联系后台"); map.put("msg", msg); map.put("path", req.getRequestURI()); map.put("params", req.getParameterMap()); map.put("status", "0"); return map; } }
加上ControllerAdvice注解,注意这个类是在controller包下面,因为spring需要扫描到,
代码中的:
@ExceptionHandler(value = Exception.class)
表示捕捉到所有的异常,你也可以捕捉一个你自定义的异常,比如:
@ExceptionHandler(BusinessException.class) @ResponseBody//这里加上这个注解才能返回json数据 public void handleBizExp(HttpServletRequest request, Exception ex){ } @ExceptionHandler(SQLException.class) public ModelAndView handSql(Exception ex){ ModelAndView mv = new ModelAndView(); return mv; }
然后我在一个接口中故意抛出一个异常:
@RestController @RequestMapping(value = "/customer",produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public class CustomerController extends BaseController{ @Autowired CustomerService customerService; @RequestMapping(value = "/getcustomer",method = RequestMethod.GET) public String getCustomer(){ logger.info(EnvironmentUtils.isTest()); List<Customer> customers = customerService.getCustomerList(); throw new KnownBizException("已知的异常"); } }
最后后台返回的数据是:
{ "msg": "已知的异常", "path": "/myschool/customer/getcustomer", "tip": "此错误说明调用接口失败,失败原因见msg,如果msg为空,联系后台", "params": {}, "status": "0" }
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- 如何优雅地处理前端异常?
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- C#异常处理详解
- 轻松学习C#的异常处理
- PHP异常处理Exception类
- JS异常处理的一个想法(sofish)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程