转发请注明:https://blog.csdn.net/somdip/article/details/86065033
package com.navitek.maternal.common;
import com.alibaba.fastjson.JSON;
import com.navitek.maternal.common.utils.ResultUtils;
import com.navitek.maternal.exception.BDException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Auther: Administrator
* @Date: 2018/8/10 0010 14:27
* @Description:
*/
@Slf4j
public class BaseController {
@ExceptionHandler
public void exp(HttpServletRequest request, HttpServletResponse response, Exception ex) throws IOException {
log.error("异常:",ex);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
PrintWriter pw = response.getWriter();
String msg = "服务器异常";
if(ex instanceof BDException){
BDException exception = (BDException) ex;
msg = exception.getMessage();
pw.print(JSON.toJSONString(ResultUtils.errorCode(exception.getCode(),msg)));
}else{
pw.print(JSON.toJSONString(ResultUtils.fail(msg)));
}
pw.flush();
pw.close();
}
}
package com.navitek.maternal.apiweb.controller;
import com.navitek.maternal.apiweb.service.SysRoleService;
import com.navitek.maternal.common.BaseController;
import com.navitek.maternal.common.utils.ResultUtils;
import com.navitek.maternal.exception.BDException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @Author: syl
* @Date: 2019/1/4 0004 14:32
* @Description:
*/
@RestController
@RequestMapping("sysRole")
@Slf4j
public class SysRoleController extends BaseController {
@Autowired
private SysRoleService sysRoleService;
@RequestMapping("selectSysRoleList")
public Map selectSysRoleList(@RequestParam Map map) {
log.info("获取角色列表");
// System.out.println(map.get("a").toString());
if(1>0){
throw new BDException("出错啦",500);
}
System.out.println(1/0);
return ResultUtils.success(sysRoleService.selectSysRoleList(map));
}
}
package com.navitek.maternal.exception;
/**
* @author 26968
*/
public class BDException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String msg;
private int code = 201;
public BDException(String msg) {
super(msg);
this.msg = msg;
}
public BDException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public BDException(String msg, int code) {
super(msg);
this.msg = msg;
this.code = code;
}
public BDException(String msg, int code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
package com.navitek.maternal.common.utils;
import java.util.HashMap;
import java.util.Map;
/**
* 结果处理工具类
*
* @author Z-Minny
*/
public abstract class ResultUtils {
/**
* 成功 00000
*/
public static Integer SUCCESS_CODE = 200;
public static String SUCCESS_MSG = "处理成功";
/**
* 失败
* 如果需要特殊的错误代码表示特定的含义 可自行添加
*/
public static Integer ERROR_CODE = 201;
public static String ERROR_MSG = "网络错误";
public static Map<String, Object> success() {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("code", SUCCESS_CODE);
resultMap.put("msg", SUCCESS_MSG);
return resultMap;
}
public static Map<String,Object> errorCode(Integer code,String msg){
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("code", code);
resultMap.put("msg", msg);
return resultMap;
}
/**
* 成功返回 包含返回信息。
* @param result 需要返回的信息。
* @return
*/
public static Map<String, Object> success(Object result) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("code", SUCCESS_CODE);
resultMap.put("msg", SUCCESS_MSG);
resultMap.put("info", result);
return resultMap;
}
/**
* 错误返回标志
* 例如 ajax请求 失败 直接返回错误。
* @return
*/
public static Map<String, Object> fail() {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("code", ERROR_CODE);
resultMap.put("msg", ERROR_MSG);
return resultMap;
}
/**
* 错误码+错误信息
* @param
* @return
*/
public static Map<String, Object> fail(String msg) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("code", ERROR_CODE);
resultMap.put("msg", msg);
return resultMap;
}
}
统一解决webapi作为接口时,后台报的所有的错误,返回规定样式,不会出现sql错误直接给前台,但是可以自定义错误
new BDexcelption("订单插入异常"),这样的信息可以返回给前端,很简单的处理方式,推荐。