去除 mvc-starter 部分功能类
This commit is contained in:
@ -1,72 +1,10 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.jpa.starter;
|
package com.chinaunicom.mall.ebtp.cloud.jpa.starter;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
import org.apache.ibatis.reflection.MetaObject;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.FilterType;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
||||||
|
|
||||||
import cn.chinaunicom.sdsi.framework.config.mybatis.MyMetaObjectHandler;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(value = "cn.chinaunicom.sdsi.framework.config.mybatis", excludeFilters = {
|
|
||||||
@Filter(type = FilterType.ASSIGNABLE_TYPE, classes = { MyMetaObjectHandler.class }) })
|
|
||||||
@PropertySource("classpath:jpa-configuration.properties")
|
@PropertySource("classpath:jpa-configuration.properties")
|
||||||
public class JpaStarterConfiguration {
|
public class JpaStarterConfiguration {
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean(MetaObjectHandler.class)
|
|
||||||
MetaObjectHandler metaObjectHandler() {
|
|
||||||
return new MetaObjectHandler() {
|
|
||||||
/**
|
|
||||||
* 更新元对象字段填充(用于更新时对公共字段的填充)
|
|
||||||
*
|
|
||||||
* @param metaObject 元对象
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void updateFill(MetaObject metaObject) {
|
|
||||||
final LocalDateTime now = LocalDateTime.now();
|
|
||||||
|
|
||||||
setFieldValByName("updateDate", now, metaObject);
|
|
||||||
setFieldValByName("lastUpdateTime", now, metaObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 插入元对象字段填充(用于插入时对公共字段的填充)
|
|
||||||
*
|
|
||||||
* @param metaObject 元对象
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void insertFill(MetaObject metaObject) {
|
|
||||||
setFieldValByName("createBy", "tester", metaObject);
|
|
||||||
|
|
||||||
// Object obj = getFieldValByName("createBy", metaObject);
|
|
||||||
// obj = getFieldValByName("createDate", metaObject);
|
|
||||||
// if (obj == null) {
|
|
||||||
// setFieldValByName("createDate", LocalDateTime.now(), metaObject);
|
|
||||||
// }
|
|
||||||
// obj = getFieldValByName("updateDate", metaObject);
|
|
||||||
// if (obj == null) {
|
|
||||||
// setFieldValByName("updateDate", LocalDateTime.now(), metaObject);
|
|
||||||
// }
|
|
||||||
// obj = getFieldValByName("tenantId", metaObject);
|
|
||||||
// if (obj == null) {
|
|
||||||
// setFieldValByName("tenantId", "ebtp_mall", metaObject);
|
|
||||||
// }
|
|
||||||
// obj = getFieldValByName("tenantName", metaObject);
|
|
||||||
// if (obj == null) {
|
|
||||||
// setFieldValByName("tenantName", "ebtp_mall", metaObject);
|
|
||||||
// }
|
|
||||||
// setFieldValByName("deleteFlag", CommonConstants.STATUS_NORMAL, metaObject);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,9 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter;
|
package com.chinaunicom.mall.ebtp.cloud.mvc.starter;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.mvc.starter.advice.BusinessExceptionHandlerAdvice;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({
|
|
||||||
BusinessExceptionHandlerAdvice.class
|
|
||||||
})
|
|
||||||
@PropertySource("classpath:mcv-configuration.properties")
|
@PropertySource("classpath:mcv-configuration.properties")
|
||||||
public class MvcStarterConfiguration {
|
public class MvcStarterConfiguration {
|
||||||
}
|
}
|
||||||
|
@ -1,254 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.advice;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.dao.DataAccessException;
|
|
||||||
import org.springframework.dao.EmptyResultDataAccessException;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.validation.BindException;
|
|
||||||
import org.springframework.validation.FieldError;
|
|
||||||
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 org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
||||||
import org.yaml.snakeyaml.constructor.DuplicateKeyException;
|
|
||||||
|
|
||||||
import com.chinaunicom.ebtp.mall.cloud.common.utils.JsonUtils;
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.mvc.starter.base.BaseResponse;
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessException;
|
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异常处理 拦截BindException异常,返回HttpStatus是400的绑定错误信息
|
|
||||||
* 拦截FrameException异常,返回HttpStatus是406的业务处理错误信息(支持自定义状态码)
|
|
||||||
* 拦截Exception异常,返回HttpStatus是500服务器内部异常
|
|
||||||
*
|
|
||||||
* @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.advice.BusinessExceptionHandlerAdvice
|
|
||||||
* @description:
|
|
||||||
* @author fqj
|
|
||||||
* @date 2020年9月3日
|
|
||||||
* @version: V1.0
|
|
||||||
* @update
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@ControllerAdvice
|
|
||||||
@ResponseBody
|
|
||||||
@ConditionalOnProperty(name = "mconfig.exception-handle-enabled", matchIfMissing = true)
|
|
||||||
public class BusinessExceptionHandlerAdvice {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 业务异常处理
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception ServiceErrorException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(value = BusinessException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> serviceErrorException(HttpServletRequest request, BusinessException exception) {
|
|
||||||
log.error(ExceptionUtil.stacktraceToString(exception));
|
|
||||||
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(exception.getCode(), exception.getMessage(), Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 参数绑定异常
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(value = BindException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> methodArgumentNotValidHandler(HttpServletRequest request, BindException exception) {
|
|
||||||
// 按需重新封装需要返回的错误信息
|
|
||||||
List<ArgumentInvalidResult> invalidArguments = new ArrayList<>();
|
|
||||||
|
|
||||||
// 解析原错误信息,封装后返回,此处返回非法的字段名称,原始值,错误信息
|
|
||||||
for (FieldError error : exception.getBindingResult().getFieldErrors()) {
|
|
||||||
ArgumentInvalidResult invalidArgument = new ArgumentInvalidResult();
|
|
||||||
invalidArgument.setDefaultMessage(error.getDefaultMessage());
|
|
||||||
invalidArgument.setField(error.getField());
|
|
||||||
invalidArgument.setRejectedValue(error.getRejectedValue());
|
|
||||||
invalidArguments.add(invalidArgument);
|
|
||||||
}
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", JsonUtils.objectToJson(invalidArguments));
|
|
||||||
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数错误", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 请求方式异常
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
||||||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
|
||||||
public BaseResponse<String> handleMethodNotSupportedException(HttpServletRequest request,
|
|
||||||
HttpRequestMethodNotSupportedException exception) {
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.METHOD_NOT_ALLOWED.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.METHOD_NOT_ALLOWED.value(), "错误的请求方式", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 参数缺失
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> handleMissingParameterException(HttpServletRequest request,
|
|
||||||
MissingServletRequestParameterException exception) {
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数缺失", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------- 数据库操作相关异常 --------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据库异常
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(DataAccessException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> handlerDataAccessException(HttpServletRequest request, DataAccessException exception) {
|
|
||||||
log.error(ExceptionUtil.stacktraceToString(exception));
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "数据库异常", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据不存在
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(EmptyResultDataAccessException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> handleDataEmptyException(HttpServletRequest request,
|
|
||||||
EmptyResultDataAccessException exception) {
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "数据不存在", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 请求方式异常
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(DuplicateKeyException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> handleDataDualException(HttpServletRequest request, DuplicateKeyException exception) {
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "数据重复插入", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 方法参数类型不匹配异常
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception BindException异常对象
|
|
||||||
* @return 响应
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
||||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
||||||
public BaseResponse<String> handleMethodArgumentTypeException(HttpServletRequest request,
|
|
||||||
MethodArgumentTypeMismatchException exception) {
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.BAD_REQUEST.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.BAD_REQUEST.value(), "参数类型不匹配", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 全局异常处理
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param exception Exception异常对象
|
|
||||||
* @return 响应 MethodArgumentNotValidException
|
|
||||||
*/
|
|
||||||
@ExceptionHandler(value = Exception.class)
|
|
||||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
||||||
public BaseResponse<String> handleException(HttpServletRequest request, Exception exception) {
|
|
||||||
log.error(ExceptionUtil.stacktraceToString(exception));
|
|
||||||
Map<String, Object> body = new HashMap<>();
|
|
||||||
body.put("errors", exception.getMessage());
|
|
||||||
body.put("error", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
|
|
||||||
body.put("path", request.getRequestURI());
|
|
||||||
|
|
||||||
return BaseResponse.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "网络异常", Convert.toStr(body));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 参数异常
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
class ArgumentInvalidResult {
|
|
||||||
/**
|
|
||||||
* 字段名
|
|
||||||
*/
|
|
||||||
private String field;
|
|
||||||
/**
|
|
||||||
* 输入的错误值
|
|
||||||
*/
|
|
||||||
private Object rejectedValue;
|
|
||||||
/**
|
|
||||||
* 错误信息
|
|
||||||
*/
|
|
||||||
private String defaultMessage;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,65 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.base;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.mvc.starter.enums.ResponseEnum;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回类型FrameResponse
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class BaseResponse<T> implements Serializable {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 6769157532172136264L;
|
|
||||||
|
|
||||||
private Integer code;
|
|
||||||
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
||||||
private T data;
|
|
||||||
|
|
||||||
public static BaseResponse<String> success() {
|
|
||||||
return new BaseResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> BaseResponse<T> success(T data) {
|
|
||||||
return new BaseResponse<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> BaseResponse<T> success(String message, T data) {
|
|
||||||
return new BaseResponse<>(ResponseEnum.SUCCESS.getCode(), message, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BaseResponse<String> fail() {
|
|
||||||
return new BaseResponse<>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getMessage(), "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BaseResponse<String> fail(String message) {
|
|
||||||
return new BaseResponse<>(ResponseEnum.ERROR.getCode(), message, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static BaseResponse<String> fail(Integer code, String message) {
|
|
||||||
return new BaseResponse<>(code, message, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> BaseResponse<T> fail(T data) {
|
|
||||||
return new BaseResponse<>(ResponseEnum.ERROR.getCode(), ResponseEnum.ERROR.getMessage(), data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> BaseResponse<T> fail(String message, T data) {
|
|
||||||
return new BaseResponse<>(ResponseEnum.ERROR.getCode(), message, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> BaseResponse<T> fail(Integer code, String message, T data) {
|
|
||||||
return new BaseResponse<T>(code, message, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.enums;
|
|
||||||
|
|
||||||
import com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessExceptionAssert;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通用异常Enum
|
|
||||||
*
|
|
||||||
* @author 付庆吉
|
|
||||||
* @date 2020-09-14
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public enum ResponseEnum implements BusinessExceptionAssert {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 成功
|
|
||||||
*/
|
|
||||||
SUCCESS(0, "success"),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 失败
|
|
||||||
*/
|
|
||||||
ERROR(90000, "操作失败"),
|
|
||||||
|
|
||||||
/**
|
|
||||||
* token not find
|
|
||||||
*/
|
|
||||||
TOKEN_NOT_FIND(30001, "认证不存在!"),
|
|
||||||
/**
|
|
||||||
* connect timeout
|
|
||||||
*/
|
|
||||||
CONNECT_TIMEOUT(30002, "连接超时!");
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回码
|
|
||||||
*/
|
|
||||||
private int code;
|
|
||||||
/**
|
|
||||||
* 返回消息
|
|
||||||
*/
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,75 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通用断言接口
|
|
||||||
*
|
|
||||||
* @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.Assert
|
|
||||||
* @description:
|
|
||||||
* @author 付庆吉
|
|
||||||
* @date 2020-09-14
|
|
||||||
* @version: V1.0
|
|
||||||
* @update
|
|
||||||
*/
|
|
||||||
public interface Assert {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建异常
|
|
||||||
*
|
|
||||||
* @param args
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
BusinessException newException(Object... args);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建异常
|
|
||||||
*
|
|
||||||
* @param t
|
|
||||||
* @param args
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
BusinessException newException(Throwable t, Object... args);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>断言对象<code>obj</code>非空。如果对象<code>obj</code>为空,则抛出异常
|
|
||||||
*
|
|
||||||
* @param obj 待判断对象
|
|
||||||
*/
|
|
||||||
default void assertNotNull(Object obj) {
|
|
||||||
if (obj == null) {
|
|
||||||
throw newException(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>断言对象<code>args</code>非空。如果对象<code>args</code>为null或空字符串,则抛出异常
|
|
||||||
*
|
|
||||||
* @param args 待判断字符串
|
|
||||||
*/
|
|
||||||
default void assertStrNotNull(String... args) {
|
|
||||||
this.assertNotNull(args);
|
|
||||||
for (String arg : args) {
|
|
||||||
if (arg == null || arg.isEmpty()) {
|
|
||||||
throw newException(arg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据传参抛出异常,则抛出异常
|
|
||||||
*
|
|
||||||
* @param t 验证结果
|
|
||||||
*/
|
|
||||||
default void customValid(boolean t) {
|
|
||||||
if (t) {
|
|
||||||
throw newException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 抛出异常
|
|
||||||
*/
|
|
||||||
default void throwException() {
|
|
||||||
throw newException();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 平台自定义异常类
|
|
||||||
*
|
|
||||||
* @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessException
|
|
||||||
* @description: 平台自定义异常类
|
|
||||||
* @author: ajaxfan
|
|
||||||
* @date: 2020-10-22
|
|
||||||
* @version: V1.0
|
|
||||||
* @update
|
|
||||||
*/
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class BusinessException extends RuntimeException {
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
private Integer code;
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
public BusinessException(IResponseEnum iResponseEnum, String message) {
|
|
||||||
super(message);
|
|
||||||
this.code = iResponseEnum.getCode();
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BusinessException(IResponseEnum iResponseEnum, String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
this.code = iResponseEnum.getCode();
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,33 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception;
|
|
||||||
|
|
||||||
import java.text.MessageFormat;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化异常及断言接口,所有自定义异常Enum均需实现此接口
|
|
||||||
* <p>
|
|
||||||
* 自定义异常枚举中年必须有:code、message
|
|
||||||
*
|
|
||||||
* @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.BusinessExceptionAssert
|
|
||||||
* @description:
|
|
||||||
* @author 付庆吉
|
|
||||||
* @date 2020-09-14
|
|
||||||
* @version: V1.0
|
|
||||||
* @update
|
|
||||||
*/
|
|
||||||
public interface BusinessExceptionAssert extends IResponseEnum, Assert {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
default BusinessException newException(Object... args) {
|
|
||||||
String msg = MessageFormat.format(this.getMessage(), args);
|
|
||||||
|
|
||||||
return new BusinessException(this, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
default BusinessException newException(Throwable t, Object... args) {
|
|
||||||
String msg = MessageFormat.format(this.getMessage(), args);
|
|
||||||
|
|
||||||
return new BusinessException(this, msg, t);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异常编码和消息
|
|
||||||
*
|
|
||||||
* @file: com.chinaunicom.mall.ebtp.cloud.mvc.starter.exception.IResponseEnum
|
|
||||||
* @description:
|
|
||||||
* @author 付庆吉
|
|
||||||
* @date 2020-09-14
|
|
||||||
* @version: V1.0
|
|
||||||
* @update
|
|
||||||
*/
|
|
||||||
public interface IResponseEnum {
|
|
||||||
|
|
||||||
int getCode();
|
|
||||||
|
|
||||||
String getMessage();
|
|
||||||
|
|
||||||
}
|
|
@ -64,6 +64,11 @@
|
|||||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||||
<version>${mybatis-plus.version}</version>
|
<version>${mybatis-plus.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-generator</artifactId>
|
||||||
|
<version>${mybatis-plus.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.pagehelper</groupId>
|
<groupId>com.github.pagehelper</groupId>
|
||||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||||
|
Reference in New Issue
Block a user