Browse Source

Merge branch 'dev-web-config' into dev

dev-minio
dashan 1 year ago
parent
commit
d541557bc2
  1. 3
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java
  2. 5
      ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java
  3. 4
      ds-commons/ds-basemodel/pom.xml
  4. 43
      ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/exception/DsException.java
  5. 38
      ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/PageResult.java
  6. 166
      ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java
  7. 11
      ds-commons/ds-config/ds-web-config/pom.xml
  8. 134
      ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java

3
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/MiniAppsApplication.java

@ -1,5 +1,6 @@
package com.ds.miniapps.biz; package com.ds.miniapps.biz;
import com.ds.commons.web.config.exection.WebGlobalExceptionHandler;
import com.ds.commons.web.config.swagger.SwaggerConfig; import com.ds.commons.web.config.swagger.SwaggerConfig;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -10,7 +11,7 @@ import org.springframework.context.annotation.Import;
* @since 2023/8/27 * @since 2023/8/27
*/ */
@SpringBootApplication @SpringBootApplication
@Import(SwaggerConfig.class) @Import({SwaggerConfig.class, WebGlobalExceptionHandler.class})
public class MiniAppsApplication { public class MiniAppsApplication {
public static void main(String[] args) { public static void main(String[] args) {

5
ds-biz/ds-miniapps/ds-miniapps-biz/src/main/java/com/ds/miniapps/biz/api/dummyinfo/DummyInfoApi.java

@ -1,5 +1,6 @@
package com.ds.miniapps.biz.api.dummyinfo; package com.ds.miniapps.biz.api.dummyinfo;
import com.ds.commons.basemodel.response.R;
import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService; import com.ds.miniapps.biz.service.dummyinfo.DummyInfoService;
import com.ds.miniapps.model.entity.dummyinfo.DummyInfo; import com.ds.miniapps.model.entity.dummyinfo.DummyInfo;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
@ -28,9 +29,9 @@ public class DummyInfoApi {
private DummyInfoService dummyInfoService; private DummyInfoService dummyInfoService;
@GetMapping("/get_list") @GetMapping("/get_list")
@Operation(summary = "获取指定数量虚拟人信息") @Operation(summary = "获取指定数量虚拟人信息")
public List<DummyInfo> getDummyInfoList( public R<List<DummyInfo>> getDummyInfoList(
@RequestParam("num") @Min(message = "数量不能小于1",value = 1) Integer num @RequestParam("num") @Min(message = "数量不能小于1",value = 1) Integer num
){ ){
return dummyInfoService.randCreateDummyInfo(num); return R.success(dummyInfoService.randCreateDummyInfo(num));
} }
} }

4
ds-commons/ds-basemodel/pom.xml

@ -23,6 +23,10 @@
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>3.3.2</version> <version>3.3.2</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

43
ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/exception/DsException.java

@ -0,0 +1,43 @@
package com.ds.commons.basemodel.exception;
import com.ds.commons.basemodel.response.R;
import lombok.*;
/**
* @author ds
* @since 2023/8/30
*/
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@Data
@AllArgsConstructor
public class DsException extends RuntimeException{
//错误码
private Integer code;
//错误信息
private String msg;
/**
* 使用R.MSG枚举定义的错误消息
* @param msg 错误消息
*/
public DsException(R.MSG msg) {
super(msg.getMsg());
this.code=msg.getCode();
this.msg=msg.getMsg();
}
public DsException append(String msg){
this.msg+=msg;
return this;
}
public DsException before(String msg){
this.msg=msg+this.msg;
return this;
}
}

38
ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/PageResult.java

@ -0,0 +1,38 @@
package com.ds.commons.basemodel.response;
import lombok.Getter;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
@Getter
public class PageResult<T> implements Serializable {
@Serial
private static final long serialVersionUID = 1402394526521183854L;
private final List<T> content;
private long page;
private long size;
private final long total;
private long totalPage;
public PageResult(List<T> content,
long page,
long size,
long total,
long totalPage){
this.content=content;
this.page=page;
this.size=size;
this.totalPage=totalPage;
this.total=total;
}
public PageResult(List<T> content,
long total){
this.content=content;
this.total=total;
}
}

166
ds-commons/ds-basemodel/src/main/java/com/ds/commons/basemodel/response/R.java

@ -0,0 +1,166 @@
package com.ds.commons.basemodel.response;
import com.ds.commons.basemodel.exception.DsException;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.io.Serializable;
import java.util.List;
/**
* Response Tool
* @author ds
*/
@ToString
@NoArgsConstructor
@Data
public class R<T> implements Serializable {
@Getter
public enum MSG {
// 成功
SUCCESS(1, "SUCCESS"),
// 失败
FAILURE(0, "FAILURE"),
/* 参数错误 1000-1999 */
PARAM_VALIDATION_FAILURE(1000,"参数无效"),
NOT_EXISTS_PARAM(1001,"参数不存在"),
/* 用户错误 2000-2999 */
/* 接口异常 3000-3999 */
SQL_EXCEPTION(3000,"SQL异常")
;
final int code;
final String msg;
MSG(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
private int code;
private String msg;
private T data;
public R<T> setMsg(String msg) {
this.msg = msg;
return this;
}
public R(MSG msg) {
this.code = msg.code;
this.msg = msg.msg;
}
/**
* 通过预设错误码和自定义错误信息创建
*
* @param code 错误码
* @param msg 错误信息
*/
private R(int code, String msg) {
this.code = code;
this.msg = msg;
}
public R(T obj) {
this.code = MSG.SUCCESS.code;
this.msg = MSG.SUCCESS.msg;
this.data = obj;
}
public R(MSG msg, T obj) {
this.code = msg.code;
this.msg = msg.msg;
this.data = obj;
}
public R(int code, String msg, T obj) {
this.code = code;
this.msg = msg;
this.data = obj;
}
public static R<Void> success() {
return new R<>(MSG.SUCCESS);
}
public static <T> R<T> success(T obj) {
return new R<>(MSG.SUCCESS, obj);
}
public static R<Void> success(int code, String msg) {
return new R<>(code, msg);
}
public static <T> R<T> success(T obj,String msg) {
return new R<>(MSG.SUCCESS.code,msg, obj);
}
public static <T> R<T> failure(T obj,String msg) {
return new R<>(MSG.FAILURE.code,msg, obj);
}
public static R<Void> failure() {
return new R<>(MSG.FAILURE);
}
public static R<Void> failure(int code, String msg) {
return new R<>(code, msg);
}
public static R<Void> failure(MSG e) {
return new R<>(e.getCode(), e.getMsg());
}
public static R<Void> failure(DsException e) {
return new R<>(e.getCode(), e.getMsg());
}
public static <T> R<T> failure(T obj) {
return new R<>(MSG.FAILURE, obj);
}
/**
* 分页信息
*
* @param content 内容列表
* @param page 当前页数
* @param size 每页数据量
* @param total 数据总量
* @param totalPage 总页数
* @return res
*/
public static <T> R<PageResult<T>> pageList(List<T> content, long page, long size, long total, long totalPage) {
PageResult<T> pr = new PageResult<>(content, page, total, size, totalPage);
return new R<>(MSG.SUCCESS, pr);
}
/**
* 分页信息
*
* @param content 内容列表
* @param total 数据总量
* @return res
*/
public static <T> R<PageResult<T>> simplePageList(List<T> content, long total) {
PageResult<T> pr = new PageResult<>(content, total);
return new R<>(MSG.SUCCESS, pr);
}
}

11
ds-commons/ds-config/ds-web-config/pom.xml

@ -54,5 +54,16 @@
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.ds</groupId>
<artifactId>ds-basemodel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.27</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

134
ds-commons/ds-config/ds-web-config/src/main/java/com/ds/commons/web/config/exection/WebGlobalExceptionHandler.java

@ -0,0 +1,134 @@
package com.ds.commons.web.config.exection;
import com.ds.commons.basemodel.exception.DsException;
import com.ds.commons.basemodel.response.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/**
* @author dss
* @since 2022/3/2
*/
@Slf4j
@RestControllerAdvice
public class WebGlobalExceptionHandler {
/**
* 请求方法错误异常处理
* @param e 异常
* @param request 请求体
* @return 响应体
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public R<Void> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
log.error(request.getMethod()+"-"+request.getRequestURI() + ":不支持当前请求方法",e);
return R.failure(HttpStatus.METHOD_NOT_ALLOWED.value(),"不支持当前请求方法");
}
/**
* SQL异常处理
* @param e 异常
* @param request 请求体
* @return 响应体
*/
@ExceptionHandler(DataAccessException.class)
public R<Void> dataAccessException(DataAccessException e,HttpServletRequest request){
log.error(request.getMethod()+"-"+request.getRequestURI()+":sql异常",e);
SQLException sqlException=(SQLException) e.getCause();
return R.failure(sqlException.getErrorCode(),"SQL异常");
}
/**
* 找不到请求参数
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public R<Void> handleMissingServletRequestParameterException(MissingServletRequestParameterException e,HttpServletRequest request){
log.error(request.getMethod()+"-"+request.getRequestURI()+":"+e.getParameterName()+"参数未找到",e);
return R.failure(R.MSG.NOT_EXISTS_PARAM).setMsg(e.getParameterName()+"参数未找到");
}
/**
* POST方法的参数效验异常处理器
*
* @param e 参数验证异常
* @return ResponseInfo
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public R<Void> parameterExceptionHandler(MethodArgumentNotValidException e,HttpServletRequest request) {
log.error(request.getMethod()+"-"+request.getRequestURI()+":参数验证失败",e);
// 获取异常信息
BindingResult exceptions = e.getBindingResult();
// 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
if (exceptions.hasErrors()) {
List<ObjectError> errors = exceptions.getAllErrors();
if (!errors.isEmpty()) {
// 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
FieldError fieldError = (FieldError) errors.get(0);
return R.failure(R.MSG.PARAM_VALIDATION_FAILURE).setMsg(fieldError.getDefaultMessage());
}
}
return R.failure(R.MSG.PARAM_VALIDATION_FAILURE);
}
/**
* 项目运行异常处理
* @param e 异常
* @param request 请求体
* @return 响应体
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public R<Void> handleException(Exception e, HttpServletRequest request) {
log.error(request.getMethod()+"-"+request.getRequestURI() + ":服务运行异常",e);
return R.failure(HttpStatus.INTERNAL_SERVER_ERROR.value(),"服务运行异常");
}
/**
* 参数验证未通过异常处理
* @param e 异常
* @param request 请求体
* @return 响应体
*/
@ExceptionHandler(ConstraintViolationException.class)
public R<Void> parameterExceptionHandler(ConstraintViolationException e,HttpServletRequest request) {
log.error(request.getMethod()+"-"+request.getRequestURI()+":参数无效",e);
List<String> list= Arrays.asList(e.getMessage().split(",").clone());
if (!list.isEmpty()){
return R.failure(R.MSG.PARAM_VALIDATION_FAILURE).setMsg(list.get(0).substring(list.get(0).indexOf(":")+2));
}
return R.failure(R.MSG.PARAM_VALIDATION_FAILURE);
}
/**
* 自定义异常处理
* @param e 异常
* @param request 请求体对象
* @return 响应体
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler({DsException.class})
public R<Void> handleException(DsException e, HttpServletRequest request) {
log.error(request.getMethod()+"-"+request.getRequestURI() + ":自定义内部异常",e);
return R.failure(e);
}
}
Loading…
Cancel
Save