首页 技术 正文
技术 2022年11月14日
0 收藏 513 点赞 5,067 浏览 3318 个字

效果区:

Spring Boot☞ 统一异常处理

Spring Boot☞ 统一异常处理

Spring Boot☞ 统一异常处理

Spring Boot☞ 统一异常处理

 代码区:

package com.wls.integrateplugs.exception.dto;public class ErrorInfo<T> {    public static final Integer OK = 200;
public static final Integer ERROR = 500; private Integer code;
private String message;
private String url;
private T data; public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} public static Integer getOK() {
return OK;
} public static Integer getERROR() {
return ERROR;
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public T getData() {
return data;
} public void setData(T data) {
this.data = data;
}}

  

package com.wls.integrateplugs.exception;import com.wls.integrateplugs.mvc.enums.RespEnum;/**
* Created by wls on 2017/8/7.
*/
public class MyException extends RuntimeException {
private Integer code; public MyException() {
} public MyException(RespEnum respEnum) {
super(respEnum.getMsg());
this.code = respEnum.getCode();
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
}
}

  

package com.wls.integrateplugs.exception.controller;import com.wls.integrateplugs.exception.MyException;
import com.wls.integrateplugs.mvc.enums.RespEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;@Controller
@RequestMapping(value = "/exception")
public class ExceptionController {
@RequestMapping("/html")
public String hello() throws Exception {
throw new Exception("发生错误");
} @RequestMapping("/json")
public String json() throws MyException {
throw new MyException(RespEnum.UNKNOW_CODE);
}
}

  

package com.wls.integrateplugs.exception.handle;import com.wls.integrateplugs.exception.MyException;
import com.wls.integrateplugs.exception.dto.ErrorInfo;
import com.wls.integrateplugs.mvc.util.http.RespUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;/**
* Created by wls on 2017/8/7.
*/
@ControllerAdvice
public class ExceptionHandle { Logger logger = LoggerFactory.getLogger(ExceptionHandle.class); /*@ExceptionHandler(value = Exception.class)
@ResponseBody
public RespUtil handle(Exception e){
// synchronized ()
if(e instanceof MyException){
MyException myException = (MyException) e;
return RespUtil.error(myException.getCode(),myException.getMessage());
}else{
logger.info("【异常信息】:{}",e);
return RespUtil.error(-1,"未知错误!");
}
}*/ @ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", req.getRequestURL());
mav.setViewName("error");
return mav;
} @ExceptionHandler(value = MyException.class)
@ResponseBody
public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
ErrorInfo<String> r = new ErrorInfo<>();
r.setMessage(e.getMessage());
r.setCode(ErrorInfo.ERROR);
r.setData("Some Data");
r.setUrl(req.getRequestURL().toString());
return r;
}}

  

<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
<meta charset="UTF-8" />
<title>统一异常处理</title>
</head>
<body>
<h1>Error Handler</h1>
<div th:text="${url}"></div>
<div th:text="${exception.message}"></div>
</body>
</html>

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,145
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,617
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,461
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,236
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,871
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,952