Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
godchao 2019-07-19 18:07:28 +08:00
commit 008eaaa05f
15 changed files with 216 additions and 476 deletions

View File

@ -153,7 +153,7 @@ public abstract class BaseBinder<T> {
*/
protected List<T> getEntityList(Wrapper queryWrapper) {
if(referencedService instanceof BaseService){
return ((BaseService)referencedService).getEntityList(queryWrapper, null);
return ((BaseService)referencedService).getEntityList(queryWrapper);
}
else{
List<T> list = referencedService.list(queryWrapper);

View File

@ -1,39 +0,0 @@
package com.diboot.core.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 异常处理类
* @author Mazhicheng
* @version 2.0
* @date 2019/01/01
*/
public class ExceptionController extends BaseController{
private static final Logger log = LoggerFactory.getLogger(ExceptionController.class);
/***
* 默认异常处理
* @param request
* @param ex
* @return
*/
@ExceptionHandler(Exception.class)
public void handleException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
// 记录日志
log.error("发生异常:", ex);
String requestUrl = (String) request.getAttribute("javax.servlet.error.request_uri");
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
Object exception = request.getAttribute("javax.servlet.error.exception");
StringBuilder sb = new StringBuilder();
sb.append("request_uri: [").append(requestUrl).append("] Error occured : ").append("status_code=").append(statusCode)
.append(";message=").append(request.getAttribute("javax.servlet.error.message")).append(";exception=").append(exception);
log.warn(sb.toString());
}
}

View File

@ -0,0 +1,79 @@
package com.diboot.core.exception;
import com.diboot.core.vo.Status;
import java.util.HashMap;
import java.util.Map;
/**
* 通用的业务异常类 BusinessException
* (json形式返回值同JsonResult便于前端统一处理)
* @author : wee
* @version : v2.0
* @Date 2019-07-11 11:10
*/
public class BusinessException extends RuntimeException {
/**
* 错误的状态
*/
private Status status;
/**
* 默认操作失败
*/
public BusinessException() {
super(Status.FAIL_OPERATION.label());
this.status = Status.FAIL_OPERATION;
}
/**
* 自定义状态码
* @param status
*/
public BusinessException(Status status) {
super(status.label());
this.status = status;
}
/**
* 自定义状态码和异常
* @param status
*/
public BusinessException(Status status, Throwable ex) {
super(status.label(), ex);
this.status = status;
}
/**
* 自定义状态码和内容提示
* @param status
* @param msg
*/
public BusinessException(Status status, String msg) {
super(status.label() + ": "+ msg);
this.status = status;
}
/**
* 自定义内容提示
* @param status
* @param msg
*/
public BusinessException(Status status, String msg, Throwable ex) {
super(status.label() + ": "+ msg, ex);
this.status = status;
}
/**
* 转换为Map
* @return
*/
public Map<String, Object> toMap(){
Map<String, Object> map = new HashMap<>();
map.put("code", status.code());
map.put("msg", getMessage());
return map;
}
}

View File

@ -1,111 +0,0 @@
package com.diboot.core.exception;
import com.diboot.core.vo.Status;
/**
* 默认rest风格异常类系统自定义rest异常可以继承该类将会被自动捕获
*
* @author : wee
* @version : v2.0
* @Date 2019-07-11 11:10
*/
public class RestException extends RuntimeException {
/**
* 错误的状态
*/
private Status status;
/**
* 错误的描述
*/
private String msg;
/**
* 错误的数据
*/
private Object data;
/**
* 默认操作失败
*/
public RestException() {
super(Status.FAIL_OPERATION.label());
this.status = Status.FAIL_OPERATION;
}
/**
* 自定义状态码
*
* @param status
*/
public RestException(Status status) {
super(status.label());
this.status = status;
}
/**
* 自定义内容提示
*
* @param status
* @param msg
*/
public RestException(Status status, String msg) {
super(status.label() + ":" + msg);
this.status = status;
this.msg = msg;
}
/**
* 自定义状态带数据
*
* @param status
* @param data
*/
public RestException(Status status, Object data) {
super(status.label());
this.status = status;
this.data = data;
}
/**
* 自定义状态自定义描述带数据
*
* @param status
* @param data
* @param msg
*/
public RestException(Status status, Object data, String msg) {
super(status.label() + ":" + msg);
this.status = status;
this.msg = msg;
this.data = data;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}

View File

@ -1,66 +0,0 @@
package com.diboot.core.exception;
import org.springframework.http.HttpStatus;
/**
* 默认web风格异常类系统自定义web异常可以继承该类将会被自动捕获
*
* @author : wee
* @version : v1.0
* @Date 2019-07-11 11:11
*/
public class WebException extends RuntimeException {
/**
* 错误页面枚举 {@link HttpStatus}
*/
private HttpStatus httpStatus;
/**
* 错误的一些信息描述用于设置{@link RuntimeException#getMessage()}
*/
private String msg;
public WebException() {
//默认跳转400页面
this.httpStatus = HttpStatus.BAD_REQUEST;
this.msg = HttpStatus.BAD_REQUEST.getReasonPhrase();
}
/**
* 自定义界面
*
* @param httpStatus
*/
public WebException(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
this.msg = httpStatus.getReasonPhrase();
}
/**
* 自定义界面: 设置自定义提示信息
*
* @param httpStatus
*/
public WebException(HttpStatus httpStatus, String msg) {
super(msg);
this.msg = msg;
this.httpStatus = httpStatus;
}
public HttpStatus getHttpStatus() {
return httpStatus;
}
public void setHttpStatus(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}

View File

@ -0,0 +1,104 @@
package com.diboot.core.handle;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.util.S;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 全局异常统一处理的默认实现
* 继承自该类并添加@ControllerAdvice注解即可自动支持兼容页面和JSON的异常处理
* @author Mazhicheng
* @version v2.0
* @date 2019/07/19
*/
public class DefaultExceptionHandler {
private final static Logger log = LoggerFactory.getLogger(DefaultExceptionHandler.class);
@Autowired
private ServerProperties serverProperties;
/**
* 统一异常处理类
* @param request
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
public Object handleException(HttpServletRequest request, Exception e) {
HttpStatus status = getStatus(request);
Map<String, Object> map = null;
if(e instanceof BusinessException){
BusinessException be = (BusinessException)e;
map = be.toMap();
}
else{
map = new HashMap<>();
map.put("code", status.value());
map.put("msg", e.getMessage());
}
if(isJsonRequest(request)) {
log.warn("JSON请求异常", e);
return new ResponseEntity<>(map, status);
}
else {
//获取错误页面
String viewName = getViewName(request, e);
map.put("exception", e);
map.put("status", status.value());
map.put("message", map.get("msg"));
map.put("timestamp", new Date());
map.remove("msg");
return new ModelAndView(viewName, map);
}
}
/**
* 获取默认的错误页面
* @param request
* @param ex
* @return
*/
protected String getViewName(HttpServletRequest request, Exception ex){
return "error";
}
/**
* 是否为JSON数据请求
* @param request
* @return
*/
private boolean isJsonRequest(HttpServletRequest request){
return S.contains(request.getHeader("Accept"),"json")
|| S.contains(request.getHeader("content-type"), "json");
}
/**
* 获取状态码
* @param request
* @return
*/
protected HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
}

View File

@ -1,88 +0,0 @@
package com.diboot.core.handle;
import com.diboot.core.exception.RestException;
import com.diboot.core.exception.WebException;
import com.diboot.core.properties.DibootProperties;
import com.diboot.core.util.S;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
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;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 异常统一捕获类只捕获{@link RestException} {@link WebException}及其子类
*
* <p>
* 如果没有特殊要求系统中可以直接抛出{@link RestException} {@link WebException}异常<br/>
* 如果想对每个异常有个具体的描述方便排查可以继承上述两个类进行异常细化描述
* </p>
* <p>
* 如果上述两个异常不满足要求可以自定义异常捕获
* </p>
*
* @author : wee
* @version : v2.0
* @Date 2019-07-11 11:13
*/
@ControllerAdvice
public class GlobalExceptionHandler {
private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@Autowired
private DibootProperties dibootProperties;
/**
* 捕获{@link RestException}及其子类异常返回{@link JsonResult}
*
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(RestException.class)
public JsonResult advice(RestException e) {
String msg = V.notEmpty(e.getMsg()) ? S.join(e.getStatus().label(), ":", e.getMsg()) : e.getStatus().label();
log.error("【rest错误】<== 错误码:{},错误信息:{}", e.getStatus().code(), msg, e);
return new JsonResult(e.getStatus(), e.getData(), (V.isEmpty(e.getMsg()) ? "" : e.getMsg()));
}
/**
* 捕获{@link WebException}及其子类异常
* <p>如果配置了自定义错误页面那么跳转到指定的错误页面否则返回spring错误页面</p>
* <p>错误页面提供页面类型{@link org.springframework.http.HttpStatus}</p>
*
* @param we
* @return
*/
@ExceptionHandler(WebException.class)
public ModelAndView advice(WebException we) {
log.error("【web错误】<==", we);
//获取配置信息
String redirectUrl = dibootProperties.getException().getPage().get(we.getHttpStatus());
if (V.notEmpty(redirectUrl)) {
//存在页面跳转至自定义页面
return new ModelAndView("redirect:" + redirectUrl);
}
//默认提示信息
Map<String, Object> model = new HashMap<>(16);
model.put("exception", we.getClass().getName());
model.put("status", we.getHttpStatus().value());
model.put("message", StringUtils.isEmpty(we.getMsg()) ? "No message available" : we.getMsg());
model.put("timestamp", new Date());
return new ModelAndView("error", model);
}
}

View File

@ -1,57 +0,0 @@
package com.diboot.core.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* 核心配置
*
* @author : wee
* @version : v2.0
* @Date 2019-07-11 14:12
*/
@EnableConfigurationProperties(DibootProperties.class)
@ConfigurationProperties(prefix = "diboot.web")
@Component
public class DibootProperties {
/**
* 异常页面配置
*/
private ExceptionProperties exception = new ExceptionProperties();
public ExceptionProperties getException() {
return exception;
}
public void setException(ExceptionProperties exception) {
this.exception = exception;
}
/**
* 错误页面配置
*
* @author : wee
* @version : v1.0
* @Date 2019-07-11 14:16
*/
public static class ExceptionProperties {
/**
* 响应状态指定页面
*/
private Map<HttpStatus, String> page;
public Map<HttpStatus, String> getPage() {
return page;
}
public void setPage(Map<HttpStatus, String> page) {
this.page = page;
}
}
}

View File

@ -75,11 +75,7 @@ public class S extends StringUtils{
* @return
*/
public static String[] toStringArray(List<String> stringList){
String[] array = new String[stringList.size()];
for(int i=0; i<stringList.size(); i++){
array[i] = stringList.get(i);
}
return array;
return stringList.toArray(new String[stringList.size()]);
}
/***

View File

@ -1,13 +1,9 @@
package com.diboot.example.controller;
import com.diboot.core.controller.BaseController;
import com.diboot.core.exception.RestException;
import com.diboot.core.exception.WebException;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;
import com.diboot.example.exception.ExampleRestException;
import com.diboot.example.exception.ExampleWebException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -23,7 +19,7 @@ import org.springframework.web.servlet.ModelAndView;
*/
@Controller
@RequestMapping("/error")
public class ErrorController extends BaseController {
public class ErrorExampleController extends BaseController {
/**
* 测试自定义
@ -34,21 +30,26 @@ public class ErrorController extends BaseController {
@ResponseBody
public JsonResult testRest(@PathVariable("num") Integer num) {
if (num == 1) {
throw new RestException();
throw new BusinessException();
}
if (num == 2) {
throw new RestException(Status.FAIL_EXCEPTION);
throw new BusinessException(Status.FAIL_EXCEPTION);
}
if (num == 3) {
throw new RestException(Status.FAIL_EXCEPTION, "自定义描述");
throw new BusinessException(Status.FAIL_EXCEPTION, "自定义描述");
}
if (num == 4) {
throw new RestException(Status.FAIL_NO_PERMISSION, num, "将传入的数据返回数据");
throw new BusinessException(Status.FAIL_NO_PERMISSION, "将传入的数据返回数据");
}
if (num == 5) {
throw new ExampleRestException(Status.FAIL_NO_PERMISSION, num, "继承rest异常");
try{
int i=2/0;
}
catch (Exception e){
throw new BusinessException(Status.FAIL_NO_PERMISSION, "继承rest异常", e);
}
}
if (num == 6) {
@ -60,19 +61,13 @@ public class ErrorController extends BaseController {
@GetMapping("/web/{num}")
public ModelAndView testWeb(@PathVariable("num") Integer num) {
if (num == 1) {
throw new WebException(HttpStatus.BAD_REQUEST);
throw new BusinessException(Status.FAIL_NO_PERMISSION);
}
if (num == 2) {
throw new WebException(HttpStatus.FORBIDDEN);
throw new BusinessException(Status.FAIL_INVALID_TOKEN);
}
if (num == 3) {
throw new WebException(HttpStatus.INTERNAL_SERVER_ERROR);
}
if (num == 4) {
throw new ExampleWebException(HttpStatus.INTERNAL_SERVER_ERROR);
}
if (num == 5) {
throw new ExampleWebException(HttpStatus.BAD_GATEWAY);
if(num == 3){
int i=2/0;
}
return new ModelAndView("redirect:/index.html");
}

View File

@ -0,0 +1,12 @@
package com.diboot.example.controller;
import com.diboot.core.handle.DefaultExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ControllerAdvice
public class GlobalExceptionHandler extends DefaultExceptionHandler {
private final static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
}

View File

@ -1,60 +0,0 @@
package com.diboot.example.exception;
import com.diboot.core.exception.RestException;
import com.diboot.core.vo.Status;
/**
* 自定义继承的rest异常
*
* @author : wee
* @version : v2.0
* @Date 2019-07-11 15:37
*/
public class ExampleRestException extends RestException {
/**
* 默认操作失败
*/
public ExampleRestException() {
}
/**
* 自定义状态码
*
* @param status
*/
public ExampleRestException(Status status) {
super(status);
}
/**
* 自定义内容提示
*
* @param status
* @param msg
*/
public ExampleRestException(Status status, String msg) {
super(status, msg);
}
/**
* 自定义状态带数据
*
* @param status
* @param data
*/
public ExampleRestException(Status status, Object data) {
super(status, data);
}
/**
* 自定义状态自定义描述带数据
*
* @param status
* @param data
* @param msg
*/
public ExampleRestException(Status status, Object data, String msg) {
super(status, data, msg);
}
}

View File

@ -1,21 +0,0 @@
package com.diboot.example.exception;
import com.diboot.core.exception.WebException;
import org.springframework.http.HttpStatus;
/**
* 自定义继承的rest异常
*
* @author : wee
* @version : v2.0
* @Date 2019-07-11 15:37
*/
public class ExampleWebException extends WebException {
public ExampleWebException() {
}
public ExampleWebException(HttpStatus httpStatus) {
super(httpStatus);
}
}

View File

@ -96,8 +96,4 @@ diboot.shiro.auth.env=dev
#------web页面访问的时候需要如下配置----
spring.mvc.view.prefix=/static
spring.mvc.view.suffix=.html
#自定义web异常页面
diboot.web.exception.page.bad-request=/400.html
diboot.web.exception.page.forbidden=/403.html
diboot.web.exception.page.internal-server-error=/405.html
spring.mvc.view.suffix=.html

View File

@ -1,6 +1,6 @@
package com.diboot.shiro.exception;
import com.diboot.core.exception.RestException;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.vo.Status;
/**
@ -9,7 +9,7 @@ import com.diboot.core.vo.Status;
* @version : v1.0
* @Date 2019-07-11 10:47
*/
public class ShiroCustomException extends RestException {
public class ShiroCustomException extends BusinessException {
public ShiroCustomException(Status status) {
super(status);