Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
3c5eff56
提交
3c5eff56
authored
11月 11, 2022
作者:
YunaiV
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化 Swagger 的实现,提升可读性
上级
b4e68fa0
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
92 行增加
和
52 行删除
+92
-52
SwaggerHandler.java
...java/cn/iocoder/yudao/gateway/swagger/SwaggerHandler.java
+20
-29
SwaggerProvider.java
...ava/cn/iocoder/yudao/gateway/swagger/SwaggerProvider.java
+70
-21
application.yaml
yudao-gateway/src/main/resources/application.yaml
+2
-2
没有找到文件。
yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/SwaggerHandler.java
浏览文件 @
3c5eff56
package
cn
.
iocoder
.
yudao
.
gateway
.
swagger
;
package
cn
.
iocoder
.
yudao
.
gateway
.
swagger
;
import
java.util.Optional
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
...
@@ -9,55 +8,47 @@ import org.springframework.web.bind.annotation.GetMapping;
...
@@ -9,55 +8,47 @@ import org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
springfox.documentation.swagger.web.
SecurityConfiguration
;
import
springfox.documentation.swagger.web.
*
;
import
springfox.documentation.swagger.web.SecurityConfigurationBuilder
;
import
springfox.documentation.swagger.web.SwaggerResourcesProvider
;
import
javax.annotation.Resource
;
import
springfox.documentation.swagger.web.UiConfiguration
;
import
java.util.List
;
import
springfox.documentation.swagger.web.UiConfigurationBuilder
;
import
java.util.Optional
;
/**
/**
* Swagger Controller
*
* @author zxliu
* @author zxliu
* @
cre
ate 2022-10-25 11:24
* @
d
ate 2022-10-25 11:24
*/
*/
@RestController
@RestController
@RequestMapping
(
"/swagger-resources"
)
@RequestMapping
(
"/swagger-resources"
)
public
class
SwaggerHandler
{
public
class
SwaggerHandler
{
@Resource
private
SwaggerResourcesProvider
swaggerResources
;
@SuppressWarnings
(
"SpringJavaAutowiredFieldsWarningInspection"
)
// 只有 @Autowired 可以实现可选注入
@Autowired
(
required
=
false
)
@Autowired
(
required
=
false
)
private
SecurityConfiguration
securityConfiguration
;
private
SecurityConfiguration
securityConfiguration
;
@SuppressWarnings
(
"SpringJavaAutowiredFieldsWarningInspection"
)
// 只有 @Autowired 可以实现可选注入
@Autowired
(
required
=
false
)
@Autowired
(
required
=
false
)
private
UiConfiguration
uiConfiguration
;
private
UiConfiguration
uiConfiguration
;
private
final
SwaggerResourcesProvider
swaggerResources
;
@GetMapping
(
""
)
public
Mono
<
ResponseEntity
<
List
<
SwaggerResource
>>>
swaggerResources
()
{
return
Mono
.
just
((
new
ResponseEntity
<>(
swaggerResources
.
get
(),
HttpStatus
.
OK
)));
@Autowired
public
SwaggerHandler
(
SwaggerResourcesProvider
swaggerResources
)
{
this
.
swaggerResources
=
swaggerResources
;
}
}
@GetMapping
(
"/configuration/security"
)
@GetMapping
(
"/configuration/security"
)
public
Mono
<
ResponseEntity
<
SecurityConfiguration
>>
securityConfiguration
()
{
public
Mono
<
ResponseEntity
<
SecurityConfiguration
>>
securityConfiguration
()
{
return
Mono
.
just
(
new
ResponseEntity
<>(
return
Mono
.
just
(
new
ResponseEntity
<>(
Optional
.
ofNullable
(
securityConfiguration
)
Optional
.
ofNullable
(
securityConfiguration
).
orElse
(
SecurityConfigurationBuilder
.
builder
().
build
()),
.
orElse
(
SecurityConfigurationBuilder
.
builder
().
build
()),
HttpStatus
.
OK
));
HttpStatus
.
OK
));
}
}
@GetMapping
(
"/configuration/ui"
)
@GetMapping
(
"/configuration/ui"
)
public
Mono
<
ResponseEntity
<
UiConfiguration
>>
uiConfiguration
()
{
public
Mono
<
ResponseEntity
<
UiConfiguration
>>
uiConfiguration
()
{
return
Mono
.
just
(
new
ResponseEntity
<>(
return
Mono
.
just
(
new
ResponseEntity
<>(
Optional
.
ofNullable
(
uiConfiguration
)
Optional
.
ofNullable
(
uiConfiguration
).
orElse
(
UiConfigurationBuilder
.
builder
().
build
()),
HttpStatus
.
OK
));
.
orElse
(
UiConfigurationBuilder
.
builder
().
build
()),
HttpStatus
.
OK
));
}
@SuppressWarnings
(
"rawtypes"
)
@GetMapping
(
""
)
public
Mono
<
ResponseEntity
>
swaggerResources
()
{
return
Mono
.
just
((
new
ResponseEntity
<>(
swaggerResources
.
get
(),
HttpStatus
.
OK
)));
}
}
}
}
yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/SwaggerProvider.java
浏览文件 @
3c5eff56
package
cn
.
iocoder
.
yudao
.
gateway
.
swagger
;
package
cn
.
iocoder
.
yudao
.
gateway
.
swagger
;
import
java.util.ArrayList
;
import
cn.hutool.core.collection.CollUtil
;
import
java.util.List
;
import
cn.hutool.core.util.StrUtil
;
import
lombok.
AllArgsConstructor
;
import
lombok.
extern.slf4j.Slf4j
;
import
org.springframework.cloud.gateway.config.GatewayProperties
;
import
org.springframework.cloud.gateway.config.GatewayProperties
;
import
org.springframework.cloud.gateway.route.RouteLocator
;
import
org.springframework.cloud.gateway.handler.predicate.PredicateDefinition
;
import
org.springframework.cloud.gateway.route.RouteDefinition
;
import
org.springframework.cloud.gateway.support.NameUtils
;
import
org.springframework.cloud.gateway.support.NameUtils
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
springfox.documentation.swagger.web.SwaggerResource
;
import
springfox.documentation.swagger.web.SwaggerResource
;
import
springfox.documentation.swagger.web.SwaggerResourcesProvider
;
import
springfox.documentation.swagger.web.SwaggerResourcesProvider
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
/**
/**
* Swagger 资源的 Provider 实现类
*
* @author zxliu
* @author zxliu
* @
cre
ate 2022-10-25 11:23
* @
d
ate 2022-10-25 11:23
*/
*/
@Component
@Component
@Primary
@Primary
@
AllArgsConstructor
@
Slf4j
public
class
SwaggerProvider
implements
SwaggerResourcesProvider
{
public
class
SwaggerProvider
implements
SwaggerResourcesProvider
{
private
final
RouteLocator
routeLocator
;
@Resource
private
final
GatewayProperties
gatewayProperties
;
private
GatewayProperties
gatewayProperties
;
/**
* 获得 SwaggerResource 列表
*
* @return SwaggerResource 列表
*/
@Override
@Override
public
List
<
SwaggerResource
>
get
()
{
public
List
<
SwaggerResource
>
get
()
{
// 将 RouteDefinition 转换成 SwaggerResource
List
<
SwaggerResource
>
resources
=
new
ArrayList
<>();
List
<
SwaggerResource
>
resources
=
new
ArrayList
<>();
List
<
String
>
routes
=
new
ArrayList
<>();
Set
<
String
>
serviceNames
=
new
HashSet
<>();
// 已处理的服务名,避免重复
routeLocator
.
getRoutes
().
subscribe
(
route
->
routes
.
add
(
route
.
getId
()));
gatewayProperties
.
getRoutes
().
forEach
(
route
->
{
gatewayProperties
.
getRoutes
().
stream
().
filter
(
routeDefinition
->
routes
.
contains
(
routeDefinition
.
getId
()))
// 已存在的服务,直接忽略
.
forEach
(
route
->
route
.
getPredicates
().
stream
()
String
serviceName
=
route
.
getUri
().
getHost
();
.
filter
(
predicateDefinition
->
(
"Path"
).
equalsIgnoreCase
(
predicateDefinition
.
getName
()))
if
(
StrUtil
.
isEmpty
(
serviceName
))
{
.
forEach
(
predicateDefinition
->
resources
.
add
(
swaggerResource
(
route
.
getId
(),
return
;
predicateDefinition
.
getArgs
().
get
(
NameUtils
.
GENERATED_NAME_PREFIX
+
"0"
)
}
.
replace
(
"**"
,
"v2/api-docs"
))))
if
(!
serviceNames
.
add
(
serviceName
))
{
);
return
;
}
return
resources
;
// 获得 Path PredicateDefinition
String
path
=
getRoutePath
(
route
);
if
(
path
==
null
)
{
return
;
}
}
// 重要:构建最终的 SwaggerResource 对象
resources
.
add
(
buildSwaggerResource
(
serviceName
,
path
));
});
return
resources
;
}
private
SwaggerResource
s
waggerResource
(
String
name
,
String
location
)
{
private
SwaggerResource
buildS
waggerResource
(
String
name
,
String
location
)
{
SwaggerResource
swaggerResource
=
new
SwaggerResource
();
SwaggerResource
swaggerResource
=
new
SwaggerResource
();
swaggerResource
.
setName
(
name
);
swaggerResource
.
setName
(
name
);
swaggerResource
.
setLocation
(
location
);
swaggerResource
.
setLocation
(
location
);
...
@@ -50,4 +72,31 @@ public class SwaggerProvider implements SwaggerResourcesProvider {
...
@@ -50,4 +72,31 @@ public class SwaggerProvider implements SwaggerResourcesProvider {
return
swaggerResource
;
return
swaggerResource
;
}
}
/**
* 获得路由的 Path
*
* ① 输入:
* predicates:
* - Path=/admin-api/system/**
* ② 输出:
* /admin-api/system/v2/api-docs
*
* @param route 路由
* @return 路由
*/
private
String
getRoutePath
(
RouteDefinition
route
)
{
PredicateDefinition
pathDefinition
=
CollUtil
.
findOne
(
route
.
getPredicates
(),
predicateDefinition
->
predicateDefinition
.
getName
().
equals
(
"Path"
));
if
(
pathDefinition
==
null
)
{
log
.
info
(
"[get][Route({}) 没有 Path 条件,忽略接口文档]"
,
route
.
getId
());
return
null
;
}
String
path
=
pathDefinition
.
getArgs
().
get
(
NameUtils
.
GENERATED_NAME_PREFIX
+
"0"
);
if
(
StrUtil
.
isEmpty
(
path
))
{
log
.
info
(
"[get][Route({}) Path 的值为空,忽略接口文档]"
,
route
.
getId
());
return
null
;
}
return
path
.
replace
(
"/**"
,
"/v2/api-docs"
);
}
}
}
yudao-gateway/src/main/resources/application.yaml
浏览文件 @
3c5eff56
...
@@ -10,9 +10,9 @@ spring:
...
@@ -10,9 +10,9 @@ spring:
-
id
:
system-admin-api
# 路由的编号
-
id
:
system-admin-api
# 路由的编号
uri
:
grayLb://system-server
uri
:
grayLb://system-server
predicates
:
# 断言,作为路由的匹配条件,对应 RouteDefinition 数组
predicates
:
# 断言,作为路由的匹配条件,对应 RouteDefinition 数组
-
Path=/admin-api/system/**
,/captcha/**
-
Path=/admin-api/system/**
filters
:
filters
:
-
RewritePath=/admin-api/system/v2/api-docs, /v2/api-docs
-
RewritePath=/admin-api/system/v2/api-docs, /v2/api-docs
# 配置,保证转发到 /v2/api-docs
-
id
:
system-app-api
# 路由的编号
-
id
:
system-app-api
# 路由的编号
uri
:
grayLb://system-server
uri
:
grayLb://system-server
predicates
:
# 断言,作为路由的匹配条件,对应 RouteDefinition 数组
predicates
:
# 断言,作为路由的匹配条件,对应 RouteDefinition 数组
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论