commit
fce0653636
16 changed files with 577 additions and 0 deletions
-
19common/pom.xml
-
33common/src/main/java/com/storeroom/exception/BaseException.java
-
23common/src/main/java/com/storeroom/exception/JsonException.java
-
19common/src/main/java/com/storeroom/exception/PageException.java
-
30common/src/main/java/com/storeroom/exception/constant/Status.java
-
27common/src/main/java/com/storeroom/exception/controller/TestController.java
-
153common/src/main/java/com/storeroom/exception/handler/ApiResponse.java
-
46common/src/main/java/com/storeroom/exception/handler/GlobalExceptionHandler.java
-
BINcommon/target/classes/com/storeroom/exception/BaseException.class
-
BINcommon/target/classes/com/storeroom/exception/JsonException.class
-
BINcommon/target/classes/com/storeroom/exception/PageException.class
-
BINcommon/target/classes/com/storeroom/exception/constant/Status.class
-
BINcommon/target/classes/com/storeroom/exception/controller/TestController.class
-
BINcommon/target/classes/com/storeroom/exception/handler/ApiResponse.class
-
BINcommon/target/classes/com/storeroom/exception/handler/GlobalExceptionHandler.class
-
227pom.xml
@ -0,0 +1,19 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<parent> |
|||
<artifactId>yxk_StoreroomSystem</artifactId> |
|||
<groupId>com.storeroom</groupId> |
|||
<version>1.0</version> |
|||
</parent> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<artifactId>common</artifactId> |
|||
|
|||
<properties> |
|||
<maven.compiler.source>17</maven.compiler.source> |
|||
<maven.compiler.target>17</maven.compiler.target> |
|||
</properties> |
|||
|
|||
</project> |
@ -0,0 +1,33 @@ |
|||
package com.storeroom.exception; |
|||
|
|||
import com.storeroom.exception.constant.Status; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
|
|||
|
|||
/** |
|||
* 统一异常类 |
|||
*/ |
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
public class BaseException extends RuntimeException{ |
|||
|
|||
private Integer code; |
|||
private String message; |
|||
|
|||
|
|||
|
|||
public BaseException(Status status){ |
|||
super(status.getMessage()); |
|||
this.code=status.getCode(); |
|||
this.message=status.getMessage(); |
|||
|
|||
} |
|||
|
|||
|
|||
public BaseException(Integer code,String message){ |
|||
super(message); |
|||
this.code=code; |
|||
this.message=message; |
|||
} |
|||
} |
@ -0,0 +1,23 @@ |
|||
package com.storeroom.exception; |
|||
|
|||
|
|||
import com.storeroom.exception.constant.Status; |
|||
import lombok.Getter; |
|||
|
|||
|
|||
/** |
|||
* Json 异常 |
|||
*/ |
|||
@Getter |
|||
public class JsonException extends BaseException { |
|||
|
|||
public JsonException(Status status) { |
|||
super(status); |
|||
} |
|||
|
|||
public JsonException(Integer code,String message) { |
|||
super(code, message); |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,19 @@ |
|||
package com.storeroom.exception; |
|||
|
|||
import com.storeroom.exception.constant.Status; |
|||
import lombok.Getter; |
|||
|
|||
/** |
|||
* 页面异常类 |
|||
*/ |
|||
@Getter |
|||
public class PageException extends BaseException { |
|||
|
|||
public PageException(Status status) { |
|||
super(status); |
|||
} |
|||
|
|||
public PageException(Integer code, String message) { |
|||
super(code, message); |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
package com.storeroom.exception.constant; |
|||
|
|||
|
|||
import lombok.Getter; |
|||
|
|||
@Getter |
|||
public enum Status { |
|||
|
|||
//操作成功 |
|||
SUCCESS(200,"操作成功"), |
|||
//操作失败 |
|||
FAIL(999,"操作失败"), |
|||
//未知异常 |
|||
UNKNOWN_ERROR(500,"服务器内部错误"); |
|||
|
|||
|
|||
//状态码 |
|||
private final Integer code; |
|||
|
|||
//内容 |
|||
private final String message; |
|||
|
|||
//构造 |
|||
Status(Integer code,String message){ |
|||
this.code=code; |
|||
this.message=message; |
|||
|
|||
} |
|||
|
|||
} |
@ -0,0 +1,27 @@ |
|||
package com.storeroom.exception.controller; |
|||
|
|||
import com.storeroom.exception.JsonException; |
|||
import com.storeroom.exception.PageException; |
|||
import com.storeroom.exception.constant.Status; |
|||
import com.storeroom.exception.handler.ApiResponse; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.servlet.ModelAndView; |
|||
|
|||
@Controller |
|||
public class TestController { |
|||
|
|||
@GetMapping |
|||
public ApiResponse<Object> jsonException(){ |
|||
throw new JsonException(Status.UNKNOWN_ERROR); |
|||
} |
|||
|
|||
public ModelAndView pageException(){ |
|||
throw new PageException(Status.UNKNOWN_ERROR); |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
} |
@ -0,0 +1,153 @@ |
|||
package com.storeroom.exception.handler; |
|||
|
|||
|
|||
import com.storeroom.exception.constant.Status; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* 通用的 API 接口封装 |
|||
*/ |
|||
@Data |
|||
public class ApiResponse<T> { |
|||
|
|||
|
|||
/** |
|||
* 状态码 |
|||
*/ |
|||
private Integer code; |
|||
|
|||
/** |
|||
* 返回类容 |
|||
*/ |
|||
private String message; |
|||
|
|||
/** |
|||
* 返回数据 |
|||
*/ |
|||
private Object data; |
|||
|
|||
/** |
|||
* 调用接口时间 |
|||
*/ |
|||
private long timestamp; |
|||
|
|||
|
|||
/** |
|||
* 无参构造 |
|||
*/ |
|||
public ApiResponse() { |
|||
//调用时间赋值 |
|||
this.timestamp = System.currentTimeMillis(); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 泛型构造 |
|||
* |
|||
* @param data |
|||
*/ |
|||
private ApiResponse(T data) { |
|||
this.code = Status.SUCCESS.getCode(); |
|||
this.message = Status.SUCCESS.getMessage(); |
|||
this.data = data; |
|||
} |
|||
|
|||
/** |
|||
* 无状态码构造 |
|||
* |
|||
* @param status |
|||
*/ |
|||
private ApiResponse(Status status) { |
|||
if (null == status) { |
|||
return; |
|||
} |
|||
this.code = status.getCode(); |
|||
this.message = status.getMessage(); |
|||
} |
|||
|
|||
/** |
|||
* 全参构造函数 |
|||
* |
|||
* @param code |
|||
* @param message |
|||
* @param data |
|||
*/ |
|||
private ApiResponse(Integer code, String message, Object data) { |
|||
this.code = code; |
|||
this.message = message; |
|||
this.data = data; |
|||
} |
|||
|
|||
/** |
|||
* 成功返回 |
|||
* |
|||
* @param data 数据 |
|||
* @param <T> |
|||
* @return |
|||
*/ |
|||
public static <T> ApiResponse<T> success(T data) { |
|||
ApiResponse<T> apiResponse = new ApiResponse<>(); |
|||
apiResponse.setCode(Status.SUCCESS.getCode()); |
|||
apiResponse.setMessage(Status.SUCCESS.getMessage()); |
|||
apiResponse.setData(data); |
|||
return apiResponse; |
|||
} |
|||
|
|||
/** |
|||
* 返回默认失败 |
|||
* |
|||
* @param code 错误代码 |
|||
* @param message 错误信息 |
|||
* @param <T> |
|||
* @return |
|||
*/ |
|||
public static <T> ApiResponse<T> error(Integer code, String message) { |
|||
ApiResponse<T> respons = new ApiResponse<>(); |
|||
respons.setCode(code); |
|||
respons.setMessage(message); |
|||
return respons; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 错误状态返回 |
|||
* |
|||
* @param status 状态码 |
|||
* @param <T> |
|||
* @return |
|||
*/ |
|||
public static <T> ApiResponse<T> error(Status status) { |
|||
return new ApiResponse<T>(status); |
|||
} |
|||
|
|||
/** |
|||
* 服务器错误失败 |
|||
* |
|||
* @param <T> |
|||
* @return |
|||
*/ |
|||
public static <T> ApiResponse<T> error() { |
|||
return error(Status.UNKNOWN_ERROR); |
|||
} |
|||
|
|||
|
|||
public Integer getCode() { |
|||
return code; |
|||
} |
|||
|
|||
public ApiResponse<T> setCode(Integer code) { |
|||
this.code = code; |
|||
return this; |
|||
} |
|||
|
|||
public Object getData(){ |
|||
return data; |
|||
} |
|||
|
|||
public ApiResponse<T> setData(T data){ |
|||
this.data=data; |
|||
return this; |
|||
} |
|||
|
|||
|
|||
} |
@ -0,0 +1,46 @@ |
|||
package com.storeroom.exception.handler; |
|||
|
|||
import com.storeroom.exception.BaseException; |
|||
import com.storeroom.exception.constant.Status; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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.RequestBody; |
|||
import org.springframework.web.bind.annotation.ResponseBody; |
|||
|
|||
import javax.servlet.http.HttpServletRequest; |
|||
|
|||
|
|||
/** |
|||
* 统一异常处理 |
|||
*/ |
|||
@ControllerAdvice |
|||
@Slf4j |
|||
public class GlobalExceptionHandler { |
|||
|
|||
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); |
|||
|
|||
|
|||
@ExceptionHandler(BaseException.class) |
|||
@ResponseBody |
|||
public ApiResponse<String> exception(Exception e){ |
|||
log.error("全局异常信息 ex={}",e.getMessage()); |
|||
return ApiResponse.error(Status.UNKNOWN_ERROR.getCode(),e.getMessage()); |
|||
} |
|||
|
|||
@ExceptionHandler(NullPointerException.class) |
|||
@ResponseBody |
|||
public ApiResponse<String> exceptionHandler(HttpServletRequest req,NullPointerException e) { |
|||
log.error("发生空指针异常!原因是:",e); |
|||
return ApiResponse.error(Status.UNKNOWN_ERROR); |
|||
} |
|||
|
|||
@ExceptionHandler(value = Exception.class) |
|||
@ResponseBody |
|||
public ApiResponse<String> exceptionHandler(HttpServletRequest req,Exception e){ |
|||
log.error("未知异常 原因是:",e); |
|||
return ApiResponse.error(Status.UNKNOWN_ERROR); |
|||
} |
|||
} |
@ -0,0 +1,227 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<groupId>com.storeroom</groupId> |
|||
<artifactId>yxk_StoreroomSystem</artifactId> |
|||
<packaging>pom</packaging> |
|||
<version>1.0</version> |
|||
<modules> |
|||
<module>common</module> |
|||
</modules> |
|||
<name>智能库房综合管理系统</name> |
|||
|
|||
<properties> |
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> |
|||
<java.version>17</java.version> |
|||
<log4jdbc.version>1.16</log4jdbc.version> |
|||
<swagger.version>2.9.2</swagger.version> |
|||
<fastjson.version>1.2.78</fastjson.version> |
|||
<druid.version>1.2.8</druid.version> |
|||
<commons-pool2.version>2.11.1</commons-pool2.version> |
|||
<mapstruct.version>1.4.2.Final</mapstruct.version> |
|||
</properties> |
|||
|
|||
|
|||
<parent> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-parent</artifactId> |
|||
<version>2.6.1</version> |
|||
</parent> |
|||
|
|||
<dependencies> |
|||
<!--Spring boot 核心--> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-data-jpa</artifactId> |
|||
</dependency> |
|||
|
|||
<!--Spring boot Web容器--> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-web</artifactId> |
|||
</dependency> |
|||
|
|||
<!--Spring boot 测试--> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-test</artifactId> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
|
|||
<!--Spring boot 安全框架--> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-security</artifactId> |
|||
</dependency> |
|||
|
|||
<!-- spring boot 缓存 --> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-cache</artifactId> |
|||
</dependency> |
|||
|
|||
<!--Spring boot Redis--> |
|||
<dependency> |
|||
<groupId>org.springframework.boot</groupId> |
|||
<artifactId>spring-boot-starter-data-redis</artifactId> |
|||
</dependency> |
|||
|
|||
<!--spring boot 集成redis所需common-pool2--> |
|||
<dependency> |
|||
<groupId>org.apache.commons</groupId> |
|||
<artifactId>commons-pool2</artifactId> |
|||
<version>${commons-pool2.version}</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.apache.commons</groupId> |
|||
<artifactId>commons-lang3</artifactId> |
|||
<version>3.12.0</version> |
|||
</dependency> |
|||
|
|||
<!--监控sql日志--> |
|||
<dependency> |
|||
<groupId>org.bgee.log4jdbc-log4j2</groupId> |
|||
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId> |
|||
<version>${log4jdbc.version}</version> |
|||
</dependency> |
|||
|
|||
<!-- Swagger UI 相关 --> |
|||
<dependency> |
|||
<groupId>io.springfox</groupId> |
|||
<artifactId>springfox-swagger2</artifactId> |
|||
<version>${swagger.version}</version> |
|||
<exclusions> |
|||
<exclusion> |
|||
<groupId>io.swagger</groupId> |
|||
<artifactId>swagger-annotations</artifactId> |
|||
</exclusion> |
|||
<exclusion> |
|||
<groupId>io.swagger</groupId> |
|||
<artifactId>swagger-models</artifactId> |
|||
</exclusion> |
|||
</exclusions> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.springfox</groupId> |
|||
<artifactId>springfox-swagger-ui</artifactId> |
|||
<version>${swagger.version}</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.swagger</groupId> |
|||
<artifactId>swagger-annotations</artifactId> |
|||
<version>1.5.21</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>io.swagger</groupId> |
|||
<artifactId>swagger-models</artifactId> |
|||
<version>1.5.21</version> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>com.github.xiaoymin</groupId> |
|||
<artifactId>swagger-bootstrap-ui</artifactId> |
|||
<version>1.9.6</version> |
|||
</dependency> |
|||
|
|||
<!--Mysql依赖包--> |
|||
<dependency> |
|||
<groupId>mysql</groupId> |
|||
<artifactId>mysql-connector-java</artifactId> |
|||
<version>8.0.27</version> |
|||
<scope>runtime</scope> |
|||
</dependency> |
|||
|
|||
<!-- druid数据源驱动 --> |
|||
<dependency> |
|||
<groupId>com.alibaba</groupId> |
|||
<artifactId>druid-spring-boot-starter</artifactId> |
|||
<version>${druid.version}</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.lionsoul</groupId> |
|||
<artifactId>ip2region</artifactId> |
|||
<version>1.7.2</version> |
|||
</dependency> |
|||
|
|||
<!--lombok插件--> |
|||
<dependency> |
|||
<groupId>org.projectlombok</groupId> |
|||
<artifactId>lombok</artifactId> |
|||
<version>1.18.22</version> |
|||
<optional>true</optional> |
|||
</dependency> |
|||
|
|||
<!-- excel工具 --> |
|||
<dependency> |
|||
<groupId>org.apache.poi</groupId> |
|||
<artifactId>poi</artifactId> |
|||
<version>3.17</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.apache.poi</groupId> |
|||
<artifactId>poi-ooxml</artifactId> |
|||
<version>3.17</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>xerces</groupId> |
|||
<artifactId>xercesImpl</artifactId> |
|||
<version>2.12.0</version> |
|||
</dependency> |
|||
|
|||
<!-- fastjson --> |
|||
<dependency> |
|||
<groupId>com.alibaba</groupId> |
|||
<artifactId>fastjson</artifactId> |
|||
<version>${fastjson.version}</version> |
|||
</dependency> |
|||
|
|||
<!--mapStruct依赖--> |
|||
<dependency> |
|||
<groupId>org.mapstruct</groupId> |
|||
<artifactId>mapstruct</artifactId> |
|||
<version>${mapstruct.version}</version> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.mapstruct</groupId> |
|||
<artifactId>mapstruct-processor</artifactId> |
|||
<version>${mapstruct.version}</version> |
|||
<scope>provided</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>javax.inject</groupId> |
|||
<artifactId>javax.inject</artifactId> |
|||
<version>1</version> |
|||
</dependency> |
|||
|
|||
<!-- Java图形验证码 --> |
|||
<dependency> |
|||
<groupId>com.github.whvcse</groupId> |
|||
<artifactId>easy-captcha</artifactId> |
|||
<version>1.6.2</version> |
|||
</dependency> |
|||
<!--升级nashorn 版本解决JDK15 以上版本验证码报错问题--> |
|||
<dependency> |
|||
<groupId>org.openjdk.nashorn</groupId> |
|||
<artifactId>nashorn-core</artifactId> |
|||
<version>15.3</version> |
|||
</dependency> |
|||
|
|||
<!-- 解析客户端操作系统、浏览器信息 --> |
|||
<dependency> |
|||
<groupId>eu.bitwalker</groupId> |
|||
<artifactId>UserAgentUtils</artifactId> |
|||
<version>1.21</version> |
|||
</dependency> |
|||
|
|||
<dependency> |
|||
<groupId>javax.persistence</groupId> |
|||
<artifactId>javax.persistence-api</artifactId> |
|||
<version>2.2</version> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
</project> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue