Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-api-boot
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-api-boot
Commits
2845147a
提交
2845147a
authored
11月 22, 2021
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upd: 更新基础代码及配置
上级
209cd8af
隐藏空白字符变更
内嵌
并排
正在显示
29 个修改的文件
包含
602 行增加
和
101 行删除
+602
-101
.editorConfig
.editorConfig
+11
-0
build.gradle
app/build.gradle
+10
-8
Application.java
app/src/main/java/com/yiring/app/Application.java
+1
-1
GlobalExceptionHandler.java
...in/java/com/yiring/app/config/GlobalExceptionHandler.java
+80
-0
BasicIdParam.java
app/src/main/java/com/yiring/app/param/BasicIdParam.java
+36
-0
BasicPaginationParam.java
.../main/java/com/yiring/app/param/BasicPaginationParam.java
+70
-0
BasicPaginationVo.java
app/src/main/java/com/yiring/app/vo/BasicPaginationVo.java
+32
-0
HelloController.java
app/src/main/java/com/yiring/app/web/HelloController.java
+1
-1
application-dev.yml
app/src/main/resources/application-dev.yml
+2
-3
application.yml
app/src/main/resources/application.yml
+5
-2
ApplicationTests.java
app/src/test/java/com/yiring/ApplicationTests.java
+2
-3
build.gradle
basic-auth/build.gradle
+3
-4
build.gradle
basic-common/build.gradle
+0
-3
build.gradle
basic-common/core/build.gradle
+8
-5
RequestAspect.java
...src/main/java/com/yiring/common/aspect/RequestAspect.java
+33
-7
DateFormatter.java
...c/main/java/com/yiring/common/constant/DateFormatter.java
+2
-3
Result.java
...mon/core/src/main/java/com/yiring/common/core/Result.java
+8
-9
Status.java
...mon/core/src/main/java/com/yiring/common/core/Status.java
+1
-1
build.gradle
basic-common/doc/build.gradle
+6
-4
package-info.java
...mon/doc/src/main/java/com/yiring/common/package-info.java
+1
-2
SwaggerConfig.java
...rc/main/java/com/yiring/common/swagger/SwaggerConfig.java
+39
-38
application-doc.yml
basic-common/doc/src/main/resources/application-doc.yml
+2
-2
build.gradle
basic-common/util/build.gradle
+5
-0
MappingSerialize.java
...main/java/com/yiring/common/jackson/MappingSerialize.java
+27
-0
ObjectMappingSerializer.java
...va/com/yiring/common/jackson/ObjectMappingSerializer.java
+65
-0
package-info.java
...on/util/src/main/java/com/yiring/common/package-info.java
+6
-0
Commons.java
...on/util/src/main/java/com/yiring/common/util/Commons.java
+82
-0
build.gradle
build.gradle
+63
-5
settings.gradle
settings.gradle
+1
-0
没有找到文件。
.editorConfig
0 → 100644
浏览文件 @
2845147a
root = true
[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
[**.yml]
indent_size = 2
app/build.gradle
浏览文件 @
2845147a
dependencies
{
dependencies
{
implementation
project
(
":basic-auth"
)
implementation
project
(
":basic-auth"
)
implementation
project
(
":basic-common:core"
)
implementation
project
(
":basic-common:core"
)
implementation
project
(
":basic-common:util"
)
implementation
project
(
":basic-common:util"
)
runtimeOnly
project
(
":basic-common:doc"
)
implementation
project
(
":basic-common:doc"
)
implementation
'org.springframework.boot:spring-boot-starter-web'
implementation
'org.springframework.boot:spring-boot-starter-web'
implementation
'org.springframework.boot:spring-boot-starter-data-jpa'
implementation
'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly
'mysql:mysql-connector-java'
runtimeOnly
'mysql:mysql-connector-java'
implementation
'io.swagger:swagger-annotations:1.6.3'
// swagger
implementation
"io.swagger:swagger-annotations:${swaggerAnnotationsVersion}"
implementation
"org.hibernate.validator:hibernate-validator:${hibernateValidatorVersion}"
}
}
app/src/main/java/com/yiring/app/Application.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
app
;
package
com
.
yiring
.
app
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
...
@@ -12,5 +13,4 @@ public class Application {
...
@@ -12,5 +13,4 @@ public class Application {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
Application
.
class
,
args
);
SpringApplication
.
run
(
Application
.
class
,
args
);
}
}
}
}
app/src/main/java/com/yiring/app/config/GlobalExceptionHandler.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
app
.
config
;
import
com.yiring.common.core.Result
;
import
com.yiring.common.core.Status
;
import
javax.validation.ConstraintViolationException
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.HttpRequestMethodNotSupportedException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/**
* 全局错误处理
*
* @author fangzhimin
* 2017年11月30日 上午11:36:31
*/
@Slf4j
@ControllerAdvice
@ResponseBody
public
class
GlobalExceptionHandler
{
/**
* 参数校验异常
*
* @param e 异常信息
* @return 统一的校验失败信息 {@link Status#BAD_REQUEST
*/
@ExceptionHandler
(
value
=
{
BindException
.
class
,
MethodArgumentNotValidException
.
class
,
ConstraintViolationException
.
class
}
)
public
Result
<
String
>
bindErrorHandler
(
Exception
e
)
{
String
error
=
"未知参数校验错误"
;
if
(
e
instanceof
ConstraintViolationException
)
{
ConstraintViolationException
ex
=
(
ConstraintViolationException
)
e
;
error
=
ex
.
getConstraintViolations
().
iterator
().
next
().
getMessage
();
}
else
{
BindingResult
result
=
null
;
if
(
e
instanceof
MethodArgumentNotValidException
)
{
result
=
((
MethodArgumentNotValidException
)
e
).
getBindingResult
();
}
else
if
(
e
instanceof
BindException
)
{
result
=
((
BindException
)
e
).
getBindingResult
();
}
if
(
result
!=
null
)
{
error
=
result
.
getAllErrors
().
iterator
().
next
().
getDefaultMessage
();
}
}
return
Result
.
no
(
Status
.
BAD_REQUEST
,
error
);
}
/**
* 不支持的HttpMethod异常
*
* @param e 异常信息
* @return 异常信息反馈 {@link Status#METHOD_NOT_ALLOWED
*/
@ExceptionHandler
(
value
=
HttpRequestMethodNotSupportedException
.
class
)
public
Result
<
String
>
httpRequestMethodNotSupportedErrorHandler
(
Exception
e
)
{
return
Result
.
no
(
Status
.
METHOD_NOT_ALLOWED
,
e
.
getMessage
());
}
/**
* 其他异常
*
* @param e 异常信息
* @return 统一的500异常信息 {@link Status#INTERNAL_SERVER_ERROR
*/
@ExceptionHandler
(
value
=
Exception
.
class
)
public
Result
<
String
>
defaultErrorHandler
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
return
Result
.
no
(
Status
.
INTERNAL_SERVER_ERROR
,
e
);
}
}
app/src/main/java/com/yiring/app/param/BasicIdParam.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
app
.
param
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
java.io.Serializable
;
import
javax.validation.Valid
;
import
javax.validation.constraints.NotNull
;
import
lombok.AccessLevel
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.FieldDefaults
;
import
lombok.experimental.SuperBuilder
;
/**
* 公共的ID查询参数类
*
* @author ifzm
* @version 0.1 2019/5/28 22:11
*/
@ApiModel
(
"公共的ID查询参数类"
)
@Valid
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults
(
level
=
AccessLevel
.
PRIVATE
)
public
class
BasicIdParam
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
8690942241103456893L
;
@ApiModelProperty
(
value
=
"id"
,
example
=
"1"
,
required
=
true
)
@NotNull
(
message
=
"id 不能为空"
)
Long
id
;
}
app/src/main/java/com/yiring/app/param/BasicPaginationParam.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
app
.
param
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
java.io.Serializable
;
import
java.util.Objects
;
import
javax.validation.constraints.DecimalMin
;
import
javax.validation.constraints.NotNull
;
import
lombok.AccessLevel
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.FieldDefaults
;
import
lombok.experimental.SuperBuilder
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Sort
;
/**
* 分页查询参数公共父类
*
* @author ifzm
* @version 0.1 2019/3/10 16:29
*/
@SuppressWarnings
({
"unused"
})
@ApiModel
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults
(
level
=
AccessLevel
.
PRIVATE
)
public
class
BasicPaginationParam
implements
Serializable
{
private
static
final
long
serialVersionUID
=
6103761701912769946L
;
@ApiModelProperty
(
value
=
"分页条数"
,
example
=
"10"
,
required
=
true
)
@NotNull
(
message
=
"分页条数不能为空"
)
@DecimalMin
(
value
=
"1"
,
message
=
"分页条数不能小于1"
)
Integer
pageSize
;
@ApiModelProperty
(
value
=
"当前页数"
,
example
=
"1"
,
required
=
true
)
@NotNull
(
message
=
"当前页数不能为空"
)
@DecimalMin
(
value
=
"1"
,
message
=
"当前页数不能小于1"
)
Integer
pageNo
;
@ApiModelProperty
(
value
=
"排序参数"
,
example
=
"id"
)
String
sortField
;
@ApiModelProperty
(
value
=
"排序方向(ASC|DESC)"
,
example
=
"DESC"
)
Sort
.
Direction
sortOrder
;
/**
* 根据参数构建分页对象
*
* @param paginationParam {@link BasicPaginationParam}
* @return Pageable
*/
public
static
Pageable
toPageable
(
BasicPaginationParam
paginationParam
)
{
if
(
paginationParam
==
null
)
{
return
Pageable
.
unpaged
();
}
Sort
sort
=
Sort
.
unsorted
();
if
(
Objects
.
nonNull
(
paginationParam
.
getSortField
()))
{
sort
=
Sort
.
by
(
new
Sort
.
Order
(
paginationParam
.
getSortOrder
(),
paginationParam
.
getSortField
()));
}
return
PageRequest
.
of
(
paginationParam
.
getPageNo
()
-
1
,
paginationParam
.
getPageSize
(),
sort
);
}
}
app/src/main/java/com/yiring/app/vo/BasicPaginationVo.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
app
.
vo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
java.io.Serializable
;
import
lombok.AccessLevel
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.FieldDefaults
;
import
lombok.experimental.SuperBuilder
;
/**
* 分页查询响应公共父类
*
* @author ifzm
* @version 0.1 2019/3/10 16:29
*/
@ApiModel
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults
(
level
=
AccessLevel
.
PRIVATE
)
public
class
BasicPaginationVo
implements
Serializable
{
private
static
final
long
serialVersionUID
=
6103761701912769946L
;
@ApiModelProperty
(
value
=
"数据总数"
,
example
=
"100"
)
Long
total
;
}
app/src/main/java/com/yiring/app/web/HelloController.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
app
.
web
;
package
com
.
yiring
.
app
.
web
;
import
com.yiring.common.core.Result
;
import
com.yiring.common.core.Result
;
...
@@ -13,5 +14,4 @@ public class HelloController {
...
@@ -13,5 +14,4 @@ public class HelloController {
public
Result
<
String
>
hello
()
{
public
Result
<
String
>
hello
()
{
return
Result
.
ok
(
"😎 Hello World!"
);
return
Result
.
ok
(
"😎 Hello World!"
);
}
}
}
}
app/src/main/resources/application-
app
.yml
→
app/src/main/resources/application-
dev
.yml
浏览文件 @
2845147a
spring
:
spring
:
datasource
:
datasource
:
url
:
jdbc:mysql://127.0.0.1:3306/basic?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
url
:
jdbc:mysql://127.0.0.1:3306/basic?useSSL=false&
allowPublicKeyRetrieval=true&
characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username
:
root
username
:
root
password
:
123456
password
:
123456
jpa
:
jpa
:
...
@@ -8,4 +8,4 @@ spring:
...
@@ -8,4 +8,4 @@ spring:
show-sql
:
true
show-sql
:
true
open-in-view
:
true
open-in-view
:
true
hibernate
:
hibernate
:
ddl-auto
:
update
ddl-auto
:
update
\ No newline at end of file
app/src/main/resources/application.yml
浏览文件 @
2845147a
...
@@ -7,5 +7,8 @@ spring:
...
@@ -7,5 +7,8 @@ spring:
application
:
application
:
name
:
"
basic-api-app"
name
:
"
basic-api-app"
profiles
:
profiles
:
include
:
auth,doc,app
include
:
auth,doc
debug
:
true
active
:
dev
# DEBUG
debug
:
false
app/src/test/java/com/yiring/ApplicationTests.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
;
package
com
.
yiring
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
...
@@ -7,7 +8,5 @@ import org.springframework.boot.test.context.SpringBootTest;
...
@@ -7,7 +8,5 @@ import org.springframework.boot.test.context.SpringBootTest;
class
ApplicationTests
{
class
ApplicationTests
{
@Test
@Test
void
contextLoads
()
{
void
contextLoads
()
{}
}
}
}
basic-auth/build.gradle
浏览文件 @
2845147a
dependencies
{
dependencies
{
implementation
project
(
':basic-common:core'
)
implementation
project
(
':basic-common:core'
)
implementation
"cn.dev33:sa-token-spring-boot-starter:${saTokenVersion}"
implementation
"cn.dev33:sa-token-spring-boot-starter:${saTokenVersion}"
}
}
\ No newline at end of file
basic-common/build.gradle
deleted
100644 → 0
浏览文件 @
209cd8af
dependencies
{
}
\ No newline at end of file
basic-common/core/build.gradle
浏览文件 @
2845147a
dependencies
{
dependencies
{
implementation
'org.aspectj:aspectjweaver:1.9.7'
implementation
project
(
":basic-common:util"
)
implementation
'io.swagger:swagger-annotations:1.6.3'
implementation
'org.springframework.boot:spring-boot-starter-web'
implementation
'org.springframework.boot:spring-boot-starter-aop'
}
implementation
'org.springframework.boot:spring-boot-starter-web'
\ No newline at end of file
// swagger
implementation
"io.swagger:swagger-annotations:${swaggerAnnotationsVersion}"
}
basic-common/core/src/main/java/com/yiring/common/aspect/RequestAspect.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
aspect
;
package
com
.
yiring
.
common
.
aspect
;
import
com.yiring.common.constant.DateFormatter
;
import
com.yiring.common.constant.DateFormatter
;
import
com.yiring.common.core.Result
;
import
com.yiring.common.core.Result
;
import
com.yiring.common.util.Commons
;
import
java.time.LocalDateTime
;
import
javax.servlet.http.HttpServletRequest
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestAttributes
;
import
java.time.LocalDateTime
;
import
org.springframework.web.context.request.RequestContextHolder
;
/**
/**
* 请求接口切面,记录接口耗时
* 请求接口切面,记录接口耗时
...
@@ -17,26 +22,47 @@ import java.time.LocalDateTime;
...
@@ -17,26 +22,47 @@ import java.time.LocalDateTime;
* @version 0.1
* @version 0.1
*/
*/
@Slf4j
@Aspect
@Aspect
@Component
@Component
public
class
RequestAspect
{
public
class
RequestAspect
{
@Pointcut
(
"@annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.ExceptionHandler)"
)
@Pointcut
(
public
void
apiPointCut
()
{
"@annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.ExceptionHandler)"
}
)
public
void
apiPointCut
()
{}
@Around
(
"apiPointCut()"
)
@Around
(
"apiPointCut()"
)
public
Object
around
(
ProceedingJoinPoint
point
)
throws
Throwable
{
public
Object
around
(
ProceedingJoinPoint
point
)
throws
Throwable
{
HttpServletRequest
request
=
getRequest
();
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
Object
result
=
point
.
proceed
();
Object
result
=
point
.
proceed
();
long
end
=
System
.
currentTimeMillis
();
long
end
=
System
.
currentTimeMillis
();
String
timestamp
=
LocalDateTime
.
now
().
format
(
DateFormatter
.
DATE_TIME
);
String
times
=
String
.
format
(
"%.3fs"
,
(
double
)
(
end
-
start
)
/
1000
);
// Print Request Log (Optional Replace: MDC)
log
.
info
(
"[Request] {} - URL: {}, IP: {}, Times: {}"
,
timestamp
,
request
.
getRequestURL
(),
Commons
.
getClientIpAddress
(
request
),
times
);
if
(
result
instanceof
Result
)
{
if
(
result
instanceof
Result
)
{
((
Result
<?>)
result
).
setTimestamp
(
LocalDateTime
.
now
().
format
(
DateFormatter
.
DATE_TIME
)
);
((
Result
<?>)
result
).
setTimestamp
(
timestamp
);
((
Result
<?>)
result
).
setTimes
(
String
.
format
(
"%.3fs"
,
(
double
)
(
end
-
start
)
/
1000
)
);
((
Result
<?>)
result
).
setTimes
(
times
);
}
}
return
result
;
return
result
;
}
}
private
HttpServletRequest
getRequest
()
{
RequestAttributes
requestAttributes
=
RequestContextHolder
.
getRequestAttributes
();
assert
requestAttributes
!=
null
;
return
(
HttpServletRequest
)
requestAttributes
.
resolveReference
(
RequestAttributes
.
REFERENCE_REQUEST
);
}
}
}
basic-common/core/src/main/java/com/yiring/common/constant/DateFormatter.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
constant
;
package
com
.
yiring
.
common
.
constant
;
import
lombok.experimental.UtilityClass
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.DateTimeFormatter
;
import
lombok.experimental.UtilityClass
;
/**
/**
* 初始化一下常用的日期格式化规则
* 初始化一下常用的日期格式化规则
...
@@ -30,5 +30,4 @@ public class DateFormatter {
...
@@ -30,5 +30,4 @@ public class DateFormatter {
* DateTimeFormatter.ofPattern("HH:mm:ss")
* DateTimeFormatter.ofPattern("HH:mm:ss")
*/
*/
public
final
DateTimeFormatter
TIME
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss"
);
public
final
DateTimeFormatter
TIME
=
DateTimeFormatter
.
ofPattern
(
"HH:mm:ss"
);
}
}
basic-common/core/src/main/java/com/yiring/common/core/Result.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
core
;
package
com
.
yiring
.
common
.
core
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
java.io.Serializable
;
import
lombok.AccessLevel
;
import
lombok.AccessLevel
;
import
lombok.Builder
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.experimental.FieldDefaults
;
import
lombok.experimental.FieldDefaults
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.Serializable
;
/**
/**
* 标准的响应对象(所有的接口响应内容格式都应该是一致的)
* 标准的响应对象(所有的接口响应内容格式都应该是一致的)
*
*
...
@@ -27,6 +27,7 @@ import java.io.Serializable;
...
@@ -27,6 +27,7 @@ import java.io.Serializable;
@Builder
@Builder
@FieldDefaults
(
level
=
AccessLevel
.
PRIVATE
)
@FieldDefaults
(
level
=
AccessLevel
.
PRIVATE
)
public
class
Result
<
T
extends
Serializable
>
implements
Serializable
{
public
class
Result
<
T
extends
Serializable
>
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
4802543396830024571L
;
private
static
final
long
serialVersionUID
=
-
4802543396830024571L
;
/**
/**
...
@@ -78,10 +79,7 @@ public class Result<T extends Serializable> implements Serializable {
...
@@ -78,10 +79,7 @@ public class Result<T extends Serializable> implements Serializable {
* @see com.yiring.common.core.Status
* @see com.yiring.common.core.Status
*/
*/
public
static
<
T
extends
Serializable
>
Result
<
T
>
ok
()
{
public
static
<
T
extends
Serializable
>
Result
<
T
>
ok
()
{
return
(
Result
<
T
>)
Result
.
builder
()
return
(
Result
<
T
>)
Result
.
builder
().
status
(
Status
.
OK
.
value
()).
message
(
Status
.
OK
.
getReasonPhrase
()).
build
();
.
status
(
Status
.
OK
.
value
())
.
message
(
Status
.
OK
.
getReasonPhrase
())
.
build
();
}
}
/**
/**
...
@@ -91,7 +89,8 @@ public class Result<T extends Serializable> implements Serializable {
...
@@ -91,7 +89,8 @@ public class Result<T extends Serializable> implements Serializable {
* @return Result
* @return Result
*/
*/
public
static
<
T
extends
Serializable
>
Result
<
T
>
ok
(
T
body
)
{
public
static
<
T
extends
Serializable
>
Result
<
T
>
ok
(
T
body
)
{
return
(
Result
<
T
>)
Result
.
builder
()
return
(
Result
<
T
>)
Result
.
builder
()
.
status
(
Status
.
OK
.
value
())
.
status
(
Status
.
OK
.
value
())
.
message
(
Status
.
OK
.
getReasonPhrase
())
.
message
(
Status
.
OK
.
getReasonPhrase
())
.
body
(
body
)
.
body
(
body
)
...
@@ -135,7 +134,8 @@ public class Result<T extends Serializable> implements Serializable {
...
@@ -135,7 +134,8 @@ public class Result<T extends Serializable> implements Serializable {
* @see Status
* @see Status
*/
*/
public
static
<
T
extends
Serializable
>
Result
<
T
>
no
(
Status
status
,
String
details
,
Throwable
error
)
{
public
static
<
T
extends
Serializable
>
Result
<
T
>
no
(
Status
status
,
String
details
,
Throwable
error
)
{
Result
<
T
>
result
=
(
Result
<
T
>)
Result
.
builder
()
Result
<
T
>
result
=
(
Result
<
T
>)
Result
.
builder
()
.
status
(
status
.
value
())
.
status
(
status
.
value
())
.
message
(
status
.
getReasonPhrase
())
.
message
(
status
.
getReasonPhrase
())
.
details
(
details
)
.
details
(
details
)
...
@@ -147,5 +147,4 @@ public class Result<T extends Serializable> implements Serializable {
...
@@ -147,5 +147,4 @@ public class Result<T extends Serializable> implements Serializable {
return
result
;
return
result
;
}
}
}
}
basic-common/core/src/main/java/com/yiring/common/core/Status.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
core
;
package
com
.
yiring
.
common
.
core
;
import
org.springframework.lang.Nullable
;
import
org.springframework.lang.Nullable
;
...
@@ -9,7 +10,6 @@ import org.springframework.lang.Nullable;
...
@@ -9,7 +10,6 @@ import org.springframework.lang.Nullable;
@SuppressWarnings
({
"unused"
})
@SuppressWarnings
({
"unused"
})
public
enum
Status
{
public
enum
Status
{
/**
/**
* 成功
* 成功
*/
*/
...
...
basic-common/doc/build.gradle
浏览文件 @
2845147a
dependencies
{
dependencies
{
implementation
project
(
":basic-common:core"
)
implementation
project
(
":basic-common:core"
)
implementation
'org.springframework.boot:spring-boot-starter-web'
implementation
'org.springframework.boot:spring-boot-starter-web'
// swagger(knife4j)
implementation
"com.github.xiaoymin:knife4j-spring-boot-starter:${knife4jVersion}"
implementation
"org.hibernate.validator:hibernate-validator:${hibernateValidatorVersion}"
// swagger(knife4j)
implementation
"com.github.xiaoymin:knife4j-spring-boot-starter:${knife4jVersion}"
}
}
basic-common/doc/src/main/java/com/yiring/common/package-info.java
浏览文件 @
2845147a
package
com
.
yiring
.
common
;
package
com
.
yiring
.
common
;
\ No newline at end of file
basic-common/doc/src/main/java/com/yiring/common/swagger/SwaggerConfig.java
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
swagger
;
package
com
.
yiring
.
common
.
swagger
;
import
com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver
;
import
com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver
;
import
com.yiring.common.core.Status
;
import
com.yiring.common.core.Status
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
import
javax.annotation.Resource
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.context.annotation.Profile
;
import
org.springframework.context.annotation.Profile
;
import
org.springframework.
http.Http
Method
;
import
org.springframework.
web.bind.annotation.Request
Method
;
import
springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration
;
import
springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration
;
import
springfox.documentation.builders.*
;
import
springfox.documentation.builders.*
;
import
springfox.documentation.schema.
ScalarType
;
import
springfox.documentation.schema.
ModelRef
;
import
springfox.documentation.service.*
;
import
springfox.documentation.service.*
;
import
springfox.documentation.spi.DocumentationType
;
import
springfox.documentation.spi.DocumentationType
;
import
springfox.documentation.spring.web.plugins.Docket
;
import
springfox.documentation.spring.web.plugins.Docket
;
import
springfox.documentation.swagger2.annotations.EnableSwagger2
;
import
springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.function.Predicate
;
import
java.util.stream.Collectors
;
/**
/**
* Swagger Config
* Swagger Config
...
@@ -34,15 +34,16 @@ import java.util.stream.Collectors;
...
@@ -34,15 +34,16 @@ import java.util.stream.Collectors;
*/
*/
@Slf4j
@Slf4j
@Profile
(
value
=
{
"de
v
"
,
"test"
,
"preview"
})
@Profile
(
value
=
{
"de
fault"
,
"dev"
,
"mock
"
,
"test"
,
"preview"
})
@EnableSwagger2
@EnableSwagger2
WebMvc
@Configuration
@Configuration
@Import
(
BeanValidatorPluginsConfiguration
.
class
)
@Import
(
BeanValidatorPluginsConfiguration
.
class
)
public
class
SwaggerConfig
{
public
class
SwaggerConfig
{
@Value
(
"${spring.application.name}"
)
@Value
(
"${spring.application.name}"
)
String
applicationName
;
String
applicationName
;
@Value
(
"${spring.server.port}"
)
@Value
(
"${server.port}"
)
String
serverPort
;
String
serverPort
;
@Resource
@Resource
...
@@ -56,29 +57,29 @@ public class SwaggerConfig {
...
@@ -56,29 +57,29 @@ public class SwaggerConfig {
private
Docket
api
(
Predicate
<
String
>
paths
)
{
private
Docket
api
(
Predicate
<
String
>
paths
)
{
log
.
info
(
"API Doc: http://localhost:{}/doc.html"
,
serverPort
);
log
.
info
(
"API Doc: http://localhost:{}/doc.html"
,
serverPort
);
String
group
=
"default"
;
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
.
groupName
(
"default"
)
.
groupName
(
group
)
.
apiInfo
(
apiInfo
())
.
apiInfo
(
apiInfo
())
.
useDefaultResponseMessages
(
false
)
.
useDefaultResponseMessages
(
false
)
.
globalRequestParameters
(
buildGlobalRequestParameters
())
.
globalOperationParameters
(
buildGlobalRequestParameters
())
.
globalResponses
(
HttpMethod
.
GET
,
buildGlobalResponseMessage
())
.
globalResponseMessage
(
RequestMethod
.
GET
,
buildGlobalResponseMessage
())
.
globalResponses
(
HttpMethod
.
POST
,
buildGlobalResponseMessage
())
.
globalResponseMessage
(
RequestMethod
.
POST
,
buildGlobalResponseMessage
())
.
globalResponseMessage
(
RequestMethod
.
DELETE
,
buildGlobalResponseMessage
())
.
globalResponseMessage
(
RequestMethod
.
PUT
,
buildGlobalResponseMessage
())
.
select
()
.
select
()
.
apis
(
RequestHandlerSelectors
.
withClassAnnotation
(
Api
.
class
))
.
apis
(
RequestHandlerSelectors
.
withClassAnnotation
(
Api
.
class
))
.
paths
(
paths
)
.
paths
(
paths
)
.
build
()
.
build
()
.
extensions
(
openApiExtensionResolver
.
build
SettingExtensions
(
));
.
extensions
(
openApiExtensionResolver
.
build
Extensions
(
group
));
}
}
private
ApiInfo
apiInfo
()
{
private
ApiInfo
apiInfo
()
{
String
url
=
"https://yiring.com"
;
return
new
ApiInfoBuilder
()
return
new
ApiInfoBuilder
()
.
title
(
"Swagger 接口文档."
)
.
title
(
"Swagger 接口文档."
)
.
description
(
applicationName
)
.
description
(
applicationName
)
.
version
(
"1.0"
)
.
version
(
"1.0"
)
.
termsOfServiceUrl
(
url
)
.
contact
(
new
Contact
(
"© YiRing"
,
"https://yiring.com"
,
"developer@yiring.com"
))
.
contact
(
new
Contact
(
"YiRing"
,
url
,
"developer@yiring.com"
))
.
build
();
.
build
();
}
}
...
@@ -87,15 +88,18 @@ public class SwaggerConfig {
...
@@ -87,15 +88,18 @@ public class SwaggerConfig {
*
*
* @return Token 等自定义全局参数/Header
* @return Token 等自定义全局参数/Header
*/
*/
private
List
<
RequestParameter
>
buildGlobalRequestParameters
()
{
private
List
<
Parameter
>
buildGlobalRequestParameters
()
{
List
<
RequestParameter
>
parameters
=
new
ArrayList
<>();
List
<
Parameter
>
parameters
=
new
ArrayList
<>();
parameters
.
add
(
new
RequestParameterBuilder
()
parameters
.
add
(
.
name
(
"Authorization"
)
new
ParameterBuilder
()
.
description
(
"Auth Token"
)
.
name
(
"Authorization"
)
.
required
(
true
)
.
description
(
"Auth Token"
)
.
in
(
ParameterType
.
HEADER
)
.
required
(
true
)
.
query
(
q
->
q
.
model
(
m
->
m
.
scalarModel
(
ScalarType
.
STRING
)))
.
parameterType
(
"header"
)
.
build
());
.
defaultValue
(
"Basic Auth Token"
)
.
modelRef
(
new
ModelRef
(
"String"
))
.
build
()
);
return
parameters
;
return
parameters
;
}
}
...
@@ -104,13 +108,10 @@ public class SwaggerConfig {
...
@@ -104,13 +108,10 @@ public class SwaggerConfig {
*
*
* @return 所有自定义状态码消息
* @return 所有自定义状态码消息
*/
*/
private
List
<
Response
>
buildGlobalResponseMessage
()
{
private
List
<
ResponseMessage
>
buildGlobalResponseMessage
()
{
return
Arrays
.
stream
(
Status
.
values
())
return
Arrays
.
map
(
status
->
new
ResponseBuilder
()
.
stream
(
Status
.
values
())
.
code
(
String
.
valueOf
(
status
.
value
()))
.
map
(
status
->
new
ResponseMessageBuilder
().
code
(
status
.
value
()).
message
(
status
.
getReasonPhrase
()).
build
())
.
description
(
status
.
getReasonPhrase
())
.
build
())
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
}
}
}
}
basic-common/doc/src/main/resources/application-doc.yml
浏览文件 @
2845147a
...
@@ -5,4 +5,5 @@ knife4j:
...
@@ -5,4 +5,5 @@ knife4j:
username
:
admin
username
:
admin
password
:
123456
password
:
123456
setting
:
setting
:
enableOpenApi
:
false
enableOpenApi
:
false
\ No newline at end of file
enableDebug
:
true
basic-common/util/build.gradle
浏览文件 @
2845147a
dependencies
{
implementation
'org.springframework.boot:spring-boot-starter-web'
implementation
"org.apache.commons:commons-lang3:${commonsLangVersion}"
}
basic-common/util/src/main/java/com/yiring/common/jackson/MappingSerialize.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
jackson
;
import
com.fasterxml.jackson.annotation.JacksonAnnotationsInside
;
import
com.fasterxml.jackson.databind.annotation.JsonSerialize
;
import
java.lang.annotation.*
;
/**
* Jackson Mapping 序列化
*
* @author Jim
* @version 0.1
* 2021/10/15 18:05
*/
@Target
({
ElementType
.
FIELD
,
ElementType
.
ANNOTATION_TYPE
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@JacksonAnnotationsInside
@JsonSerialize
(
using
=
ObjectMappingSerializer
.
class
)
@Documented
public
@interface
MappingSerialize
{
/**
* 处理简单 Mapping 数据字典转换输出,常用于枚举数据处理
* eg: @MappingSerialize(mapping = "0:女,1:男")
*/
String
mapping
();
}
basic-common/util/src/main/java/com/yiring/common/jackson/ObjectMappingSerializer.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
jackson
;
import
com.fasterxml.jackson.core.JsonGenerator
;
import
com.fasterxml.jackson.databind.BeanProperty
;
import
com.fasterxml.jackson.databind.JsonSerializer
;
import
com.fasterxml.jackson.databind.SerializerProvider
;
import
com.fasterxml.jackson.databind.ser.ContextualSerializer
;
import
com.fasterxml.jackson.databind.ser.std.StdSerializer
;
import
java.io.IOException
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Objects
;
import
org.apache.commons.lang3.StringUtils
;
/**
* Mapping 序列化
*
* @author Jim
* @version 0.1
* 2021/10/15 18:11
*/
public
class
ObjectMappingSerializer
extends
StdSerializer
<
Object
>
implements
ContextualSerializer
{
private
static
final
long
serialVersionUID
=
-
1924851196524453827L
;
protected
ObjectMappingSerializer
()
{
super
(
Object
.
class
);
}
private
final
Map
<
String
,
String
>
mapping
=
new
HashMap
<>();
public
void
putAll
(
Map
<
String
,
String
>
map
)
{
this
.
mapping
.
putAll
(
map
);
}
@Override
public
JsonSerializer
<?>
createContextual
(
SerializerProvider
prov
,
BeanProperty
property
)
{
MappingSerialize
mappingSerialize
=
property
.
getAnnotation
(
MappingSerialize
.
class
);
String
mapping
=
mappingSerialize
.
mapping
();
Map
<
String
,
String
>
map
=
new
HashMap
<>();
if
(!
StringUtils
.
isEmpty
(
mapping
))
{
String
[]
strings
=
mapping
.
split
(
","
);
for
(
String
str
:
strings
)
{
String
[]
split
=
str
.
split
(
":"
);
map
.
put
(
split
[
0
],
split
[
1
]);
}
}
ObjectMappingSerializer
serializer
=
new
ObjectMappingSerializer
();
serializer
.
putAll
(
map
);
return
serializer
;
}
@Override
public
void
serialize
(
Object
value
,
JsonGenerator
gen
,
SerializerProvider
provider
)
throws
IOException
{
String
text
=
mapping
.
get
(
Objects
.
toString
(
value
));
if
(
StringUtils
.
isEmpty
(
text
))
{
gen
.
writeObject
(
value
);
}
else
{
gen
.
writeString
(
text
);
}
}
}
basic-common/util/src/main/java/com/yiring/common/package-info.java
0 → 100644
浏览文件 @
2845147a
/**
* @author Jim
* @version 0.1
* 2021/11/22 13:03
*/
package
com
.
yiring
.
common
;
basic-common/util/src/main/java/com/yiring/common/util/Commons.java
0 → 100644
浏览文件 @
2845147a
/* (C) 2021 YiRing, Inc. */
package
com
.
yiring
.
common
.
util
;
import
java.util.Collection
;
import
java.util.Map
;
import
java.util.UUID
;
import
javax.servlet.http.HttpServletRequest
;
import
lombok.experimental.UtilityClass
;
/**
* 公共工具类
*
* @author ifzm
* @version 0.1 2019/3/5 20:29
*/
@SuppressWarnings
({
"unused"
})
@UtilityClass
public
class
Commons
{
/** 代理 IP 请求头 */
private
static
final
String
[]
HEADERS_TO_TRY
=
{
"X-Forwarded-For"
,
"Proxy-Client-IP"
,
"WL-Proxy-Client-IP"
,
"HTTP_X_FORWARDED_FOR"
,
"HTTP_X_FORWARDED"
,
"HTTP_X_CLUSTER_CLIENT_IP"
,
"HTTP_CLIENT_IP"
,
"HTTP_FORWARDED_FOR"
,
"HTTP_FORWARDED"
,
"HTTP_VIA"
,
"REMOTE_ADDR"
,
"X-Real-IP"
,
};
/***
* 获取客户端ip地址(可以穿透代理)
*
* @param request HttpServletRequest
* @return IP
*/
public
String
getClientIpAddress
(
HttpServletRequest
request
)
{
for
(
String
header
:
HEADERS_TO_TRY
)
{
String
ip
=
request
.
getHeader
(
header
);
if
(
ip
!=
null
&&
ip
.
length
()
!=
0
&&
!
"unknown"
.
equalsIgnoreCase
(
ip
))
{
return
ip
;
}
}
return
request
.
getRemoteAddr
();
}
/**
* 随机生成一个UUID
*
* @return UUID 字符串
*/
public
String
uuid
()
{
return
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
);
}
/**
* 判断集合是否为空
*
* @param collection 集合
* @return 是否为空
*/
public
boolean
isNullOrEmpty
(
Collection
<?>
collection
)
{
return
collection
==
null
||
collection
.
isEmpty
();
}
/**
* 判断Map是否为空
*
* @param map Map
* @return 是否为空
*/
public
boolean
isNullOrEmpty
(
Map
<?,
?>
map
)
{
return
map
==
null
||
map
.
isEmpty
();
}
}
build.gradle
浏览文件 @
2845147a
buildscript
{
buildscript
{
repositories
{
maven
{
url
"https://plugins.gradle.org/m2/"
}
}
ext
{
ext
{
knife4jVersion
=
'3.0.3'
// https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter
saTokenVersion
=
'1.27.0'
knife4jVersion
=
'2.0.9'
// https://mvnrepository.com/artifact/io.swagger/swagger-annotations
swaggerAnnotationsVersion
=
'1.6.3'
// https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator
hibernateValidatorVersion
=
'6.0.22.Final'
// https://mvnrepository.com/artifact/cn.dev33/sa-token-spring-boot-starter
saTokenVersion
=
'1.28.0'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
commonsLangVersion
=
'3.12.0'
}
}
}
}
plugins
{
plugins
{
id
'org.springframework.boot'
version
'2.5.
6
'
id
'org.springframework.boot'
version
'2.5.
7
'
id
'io.spring.dependency-management'
version
'1.0.11.RELEASE'
id
'io.spring.dependency-management'
version
'1.0.11.RELEASE'
id
'java'
id
'java'
// https://plugins.gradle.org/plugin/com.diffplug.spotless
id
"com.diffplug.spotless"
version
"6.0.0"
// https://plugins.gradle.org/plugin/com.github.spotbugs
id
"com.github.spotbugs"
version
"4.7.10"
}
}
group
=
'com.yiring'
group
=
'com.yiring'
...
@@ -27,6 +42,8 @@ subprojects {
...
@@ -27,6 +42,8 @@ subprojects {
apply
plugin:
'org.springframework.boot'
apply
plugin:
'org.springframework.boot'
apply
plugin:
'io.spring.dependency-management'
apply
plugin:
'io.spring.dependency-management'
apply
plugin:
'java'
apply
plugin:
'java'
apply
plugin:
"com.diffplug.spotless"
apply
plugin:
"com.github.spotbugs"
configurations
{
configurations
{
compileOnly
{
compileOnly
{
...
@@ -55,4 +72,46 @@ subprojects {
...
@@ -55,4 +72,46 @@ subprojects {
test
{
test
{
useJUnitPlatform
()
useJUnitPlatform
()
}
}
}
\ No newline at end of file
// skip Test tasks
gradle
.
taskGraph
.
whenReady
{
tasks
.
each
{
task
->
if
(
task
.
name
.
contains
(
"test"
))
{
task
.
enabled
=
false
}
}
}
spotless
{
format
'misc'
,
{
target
'*.gradle'
,
'*.md'
,
'.gitignore'
trimTrailingWhitespace
()
indentWithTabs
()
endWithNewline
()
}
java
{
importOrder
()
removeUnusedImports
()
licenseHeader
'/* (C) $YEAR YiRing, Inc. */'
// https://www.npmjs.com/package/prettier
// https://www.npmjs.com/package/prettier-plugin-java
prettier
([
'prettier'
:
'2.4.1'
,
'prettier-plugin-java'
:
'1.6.0'
]).
config
([
'parser'
:
'java'
,
'tabWidth'
:
4
,
'printWidth'
:
120
,
'endOfLine'
:
'lf'
])
}
}
}
// GitHook pre-commit (spotless, spotbugs)
def
hook
=
new
File
(
"$rootProject.projectDir/.git/hooks/pre-commit"
)
hook
.
text
=
"""#!/bin/bash
#set -x
./gradlew spotlessCheck spotbugsMain
RESULT=\$?
exit \$RESULT
"""
settings.gradle
浏览文件 @
2845147a
pluginManagement
{
pluginManagement
{
repositories
{
repositories
{
maven
{
url
"https://maven.aliyun.com/repository/gradle-plugin"
}
maven
{
url
"https://maven.aliyun.com/repository/gradle-plugin"
}
gradlePluginPortal
()
}
}
}
}
rootProject
.
name
=
'basic'
rootProject
.
name
=
'basic'
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论