博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从零搭建自己的SpringBoot后台框架(五)
阅读量:6893 次
发布时间:2019-06-27

本文共 5840 字,大约阅读时间需要 19 分钟。

Hello大家好,本章我们添加全局异常处理。有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢

一:为什么需要定义全局异常

在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,所以我们需要对异常进行捕获,然后给予相应的处理,来减少程序异常对用户体验的影响

二:添加业务类异常

在前面说过的ret文件夹下创建ServiceException

package com.example.demo.core.ret;import java.io.Serializable;/** * @Description: 业务类异常 * @author 张瑶 * @date 2018/4/20 14:30 *  */public class ServiceException extends RuntimeException implements Serializable{   private static final long serialVersionUID = 1213855733833039552L;   public ServiceException() {   }   public ServiceException(String message) {      super(message);   }   public ServiceException(String message, Throwable cause) {      super(message, cause);   }}复制代码

三:添加异常处理配置

打开创建的WebConfigurer,添加如下方法

@Overridepublic void configureHandlerExceptionResolvers(List
exceptionResolvers) { exceptionResolvers.add(getHandlerExceptionResolver());}/** * 创建异常处理 * @return */private HandlerExceptionResolver getHandlerExceptionResolver(){ HandlerExceptionResolver handlerExceptionResolver = new HandlerExceptionResolver(){ @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { RetResult
result = getResuleByHeandleException(request, handler, e); responseResult(response, result); return new ModelAndView(); } }; return handlerExceptionResolver;}/** * 根据异常类型确定返回数据 * @param request * @param handler * @param e * @return */private RetResult getResuleByHeandleException(HttpServletRequest request, Object handler, Exception e){ RetResult result = new RetResult<>(); if (e instanceof ServiceException) { result.setCode(RetCode.FAIL).setMsg(e.getMessage()).setData(null); return result; } if (e instanceof NoHandlerFoundException) { result.setCode(RetCode.NOT_FOUND).setMsg("接口 [" + request.getRequestURI() + "] 不存在"); return result; } result.setCode(RetCode.INTERNAL_SERVER_ERROR).setMsg("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员"); String message; if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", request.getRequestURI(), handlerMethod.getBean().getClass().getName(), handlerMethod.getMethod() .getName(), e.getMessage()); } else { message = e.getMessage(); } LOGGER.error(message, e); return result;}/** * @Title: responseResult * @Description: 响应结果 * @param response * @param result * @Reutrn void */private void responseResult(HttpServletResponse response, RetResult result) { response.setCharacterEncoding("UTF-8"); response.setHeader("Content-type", "application/json;charset=UTF-8"); response.setStatus(200); try { response.getWriter().write(JSON.toJSONString(result,SerializerFeature.WriteMapNullValue)); } catch (IOException ex) { LOGGER.error(ex.getMessage()); }}复制代码

四:添加配置文件

Spring Boot 中, 当用户访问了一个不存在的链接时, Spring 默认会将页面重定向到 **/error** 上, 而不会抛出异常. 

既然如此, 那我们就告诉 Spring Boot, 当出现 404 错误时, 抛出一个异常即可. 

application.properties 中添加两个配置: 

spring.mvc.throw-exception-if-no-handler-found=true 

spring.resources.add-mappings=false 

上面的配置中, 第一个 spring.mvc.throw-exception-if-no-handler-found 告诉 SpringBoot 当出现 404 错误时, 直接抛出异常. 第二个 spring.resources.add-mappings 告诉 SpringBoot 不要为我们工程中的资源文件建立映射.

五:创建测试接口

package com.example.demo.service.impl;import com.example.demo.core.ret.ServiceException;import com.example.demo.dao.UserInfoMapper;import com.example.demo.model.UserInfo;import com.example.demo.service.UserInfoService;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.List;/** * @author 张瑶 * @Description: * @time 2018/4/18 11:56 */@Servicepublic class UserInfoServiceImpl implements UserInfoService{    @Resource    private UserInfoMapper userInfoMapper;    @Override    public UserInfo selectById(Integer id){        UserInfo userInfo = userInfoMapper.selectById(id);        if(userInfo == null){            throw new ServiceException("暂无该用户");        }        return userInfo;    }}复制代码

UserInfoController.java

package com.example.demo.controller;import com.example.demo.core.ret.RetResponse;import com.example.demo.core.ret.RetResult;import com.example.demo.core.ret.ServiceException;import com.example.demo.model.UserInfo;import com.example.demo.service.UserInfoService;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.util.List;/** * @author 张瑶 * @Description: * @time 2018/4/18 11:39 */@RestController@RequestMapping("userInfo")public class UserInfoController {    @Resource    private UserInfoService userInfoService;    @PostMapping("/hello")    public String hello(){        return "hello SpringBoot";    }    @PostMapping("/selectById")    public RetResult
selectById(Integer id){ UserInfo userInfo = userInfoService.selectById(id); return RetResponse.makeOKRsp(userInfo); } @PostMapping("/testException") public RetResult
testException(Integer id){ List a = null; a.size(); UserInfo userInfo = userInfoService.selectById(id); return RetResponse.makeOKRsp(userInfo); }}复制代码

六:接口测试

访问192.168.1.104:8080/userInfo/selectById参数 id:3

{    "code": 400,    "data": null,    "msg": "暂无该用户"}复制代码

访问192.168.1.104:8080/userInfo/testException

{    "code": 500,    "data": null,    "msg": "接口 [/userInfo/testException] 内部错误,请联系管理员"}复制代码

项目地址

码云地址:

GitHub地址:

写文章不易,如对您有帮助,请帮忙点下star

结尾

springboot添加全局异常处理已完成,后续功能接下来陆续更新,有问题可以联系我mr_beany@163.com。另求各路大神指点,感谢大家。

转载地址:http://oshbl.baihongyu.com/

你可能感兴趣的文章
Android 动态改变高度以及计算长度的EditText
查看>>
PostgreSQL获取table名,字段名
查看>>
Git(分布式版本控制系统)在Windows下的使用-将代码托管到开源中国(oschina)
查看>>
糟蹋好题——魔方阵问题
查看>>
illustrate ARC with graphs
查看>>
版本管理 - Git
查看>>
待看东西
查看>>
创建和使用RMAN存储脚本
查看>>
杂七杂八的文档资料。
查看>>
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 访问频率限制功能实现、防止黑客扫描、防止恶意刷屏...
查看>>
eclipse中项目右上方有一个s,eclipse中项目左下方友谊个红色的叉,eclipse中项目左下方友谊个红色的感叹号...
查看>>
gis数据格式转换(数据导入)ConvertFeaCls
查看>>
启发式算法(Heuristic Algorithm)
查看>>
李洪强iOS学习交流群-iOS大神群
查看>>
Netty权威指南之NIO通信模型
查看>>
Java静态分派与动态分派(二)
查看>>
How to: Set Tomato Firmware for Wireless Client Modes
查看>>
Eclipse上GIT插件EGIT使用手册之八_解决推送冲突
查看>>
CSS 技巧
查看>>
【剑指Offer学习】【面试题19 :二叉树的镜像】
查看>>