Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
陈文顺
discovery
Commits
71ab1187
Commit
71ab1187
authored
Nov 16, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持Swagger多包扫描路径
parent
e2eb4057
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
6 deletions
+92
-6
discovery-console/src/main/java/com/nepxion/discovery/console/configuration/SwaggerConfiguration.java
+46
-3
discovery-plugin-admin-center/src/main/java/com/nepxion/discovery/plugin/admincenter/configuration/SwaggerConfiguration.java
+46
-3
No files found.
discovery-console/src/main/java/com/nepxion/discovery/console/configuration/SwaggerConfiguration.java
View file @
71ab1187
...
@@ -9,15 +9,16 @@ package com.nepxion.discovery.console.configuration;
...
@@ -9,15 +9,16 @@ package com.nepxion.discovery.console.configuration;
* @version 1.0
* @version 1.0
*/
*/
import
springfox.documentation.RequestHandler
;
import
springfox.documentation.builders.ApiInfoBuilder
;
import
springfox.documentation.builders.ApiInfoBuilder
;
import
springfox.documentation.builders.PathSelectors
;
import
springfox.documentation.builders.PathSelectors
;
import
springfox.documentation.builders.RequestHandlerSelectors
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.service.Contact
;
import
springfox.documentation.service.Contact
;
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.EnableSwagger2
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
...
@@ -25,14 +26,20 @@ import org.springframework.context.annotation.Configuration;
...
@@ -25,14 +26,20 @@ import org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
com.google.common.base.Function
;
import
com.google.common.base.Optional
;
import
com.google.common.base.Predicate
;
@Configuration
@Configuration
@EnableSwagger2
@EnableSwagger2
@ConditionalOnProperty
(
value
=
"swagger.service.enabled"
,
matchIfMissing
=
true
)
@ConditionalOnProperty
(
value
=
"swagger.service.enabled"
,
matchIfMissing
=
true
)
public
class
SwaggerConfiguration
implements
WebMvcConfigurer
{
public
class
SwaggerConfiguration
implements
WebMvcConfigurer
{
public
static
final
String
BASE_PACKAGE
=
"com.nepxion.discovery.console.endpoint"
;
@Value
(
"${spring.application.name}"
)
@Value
(
"${spring.application.name}"
)
private
String
serviceName
;
private
String
serviceName
;
@Value
(
"${swagger.service.base.package:
com.nepxion.discovery.console.endpoint
}"
)
@Value
(
"${swagger.service.base.package:}"
)
private
String
basePackage
;
private
String
basePackage
;
@Value
(
"${swagger.service.description:Console Restful APIs}"
)
@Value
(
"${swagger.service.description:Console Restful APIs}"
)
...
@@ -56,6 +63,9 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -56,6 +63,9 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
@Value
(
"${swagger.service.contact.email:1394997@qq.com}"
)
@Value
(
"${swagger.service.contact.email:1394997@qq.com}"
)
private
String
contactEmail
;
private
String
contactEmail
;
@Value
(
"${swagger.service.termsOfServiceUrl:http://www.nepxion.com"
)
private
String
termsOfServiceUrl
;
@Value
(
"${swagger.cors.registry.enabled:true}"
)
@Value
(
"${swagger.cors.registry.enabled:true}"
)
private
Boolean
corsRegistryEnabled
;
private
Boolean
corsRegistryEnabled
;
...
@@ -64,7 +74,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -64,7 +74,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
.
apiInfo
(
apiInfo
())
.
apiInfo
(
apiInfo
())
.
select
()
.
select
()
.
apis
(
RequestHandlerSelectors
.
basePackage
(
basePackage
))
// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.
apis
(
SwaggerConfiguration
.
basePackage
(
BASE_PACKAGE
+
(
StringUtils
.
isNotEmpty
(
basePackage
.
trim
())
?
","
+
basePackage
.
trim
()
:
StringUtils
.
EMPTY
)
))
// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.
paths
(
PathSelectors
.
any
())
.
paths
(
PathSelectors
.
any
())
.
build
();
.
build
();
}
}
...
@@ -77,6 +87,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -77,6 +87,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
.
license
(
license
)
.
license
(
license
)
.
licenseUrl
(
licenseUrl
)
.
licenseUrl
(
licenseUrl
)
.
contact
(
new
Contact
(
contactName
,
contactUrl
,
contactEmail
))
.
contact
(
new
Contact
(
contactName
,
contactUrl
,
contactEmail
))
.
termsOfServiceUrl
(
termsOfServiceUrl
)
.
build
();
.
build
();
}
}
...
@@ -90,4 +101,35 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -90,4 +101,35 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
.
allowedOrigins
(
"*"
);
.
allowedOrigins
(
"*"
);
}
}
}
}
public
static
Predicate
<
RequestHandler
>
basePackage
(
String
basePackage
)
{
return
new
Predicate
<
RequestHandler
>()
{
@Override
public
boolean
apply
(
RequestHandler
input
)
{
return
declaringClass
(
input
).
transform
(
handlerPackage
(
basePackage
)).
or
(
true
);
}
};
}
private
static
Function
<
Class
<?>,
Boolean
>
handlerPackage
(
String
basePackage
)
{
return
new
Function
<
Class
<?>,
Boolean
>()
{
@Override
public
Boolean
apply
(
Class
<?>
input
)
{
String
[]
subPackages
=
basePackage
.
split
(
","
);
for
(
String
subPackage
:
subPackages
)
{
boolean
matched
=
input
.
getPackage
().
getName
().
startsWith
(
subPackage
.
trim
());
if
(
matched
)
{
return
true
;
}
}
return
false
;
}
};
}
@SuppressWarnings
(
"deprecation"
)
private
static
Optional
<?
extends
Class
<?>>
declaringClass
(
RequestHandler
input
)
{
return
Optional
.
fromNullable
(
input
.
declaringClass
());
}
}
}
\ No newline at end of file
discovery-plugin-admin-center/src/main/java/com/nepxion/discovery/plugin/admincenter/configuration/SwaggerConfiguration.java
View file @
71ab1187
...
@@ -9,15 +9,16 @@ package com.nepxion.discovery.plugin.admincenter.configuration;
...
@@ -9,15 +9,16 @@ package com.nepxion.discovery.plugin.admincenter.configuration;
* @version 1.0
* @version 1.0
*/
*/
import
springfox.documentation.RequestHandler
;
import
springfox.documentation.builders.ApiInfoBuilder
;
import
springfox.documentation.builders.ApiInfoBuilder
;
import
springfox.documentation.builders.PathSelectors
;
import
springfox.documentation.builders.PathSelectors
;
import
springfox.documentation.builders.RequestHandlerSelectors
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.service.ApiInfo
;
import
springfox.documentation.service.Contact
;
import
springfox.documentation.service.Contact
;
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.EnableSwagger2
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
...
@@ -26,15 +27,21 @@ import org.springframework.context.annotation.Configuration;
...
@@ -26,15 +27,21 @@ import org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.CorsRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
import
com.google.common.base.Function
;
import
com.google.common.base.Optional
;
import
com.google.common.base.Predicate
;
@Configuration
@Configuration
@EnableSwagger2
@EnableSwagger2
@ConditionalOnClass
(
name
=
{
"javax.servlet.ServletContext"
})
// 适配Spring Cloud Api Gateway,不装载Swagger
@ConditionalOnClass
(
name
=
{
"javax.servlet.ServletContext"
})
// 适配Spring Cloud Api Gateway,不装载Swagger
@ConditionalOnProperty
(
value
=
"swagger.service.enabled"
,
matchIfMissing
=
true
)
@ConditionalOnProperty
(
value
=
"swagger.service.enabled"
,
matchIfMissing
=
true
)
public
class
SwaggerConfiguration
implements
WebMvcConfigurer
{
public
class
SwaggerConfiguration
implements
WebMvcConfigurer
{
public
static
final
String
BASE_PACKAGE
=
"com.nepxion.discovery.plugin.admincenter.endpoint"
;
@Value
(
"${spring.application.name}"
)
@Value
(
"${spring.application.name}"
)
private
String
serviceName
;
private
String
serviceName
;
@Value
(
"${swagger.service.base.package:
com.nepxion.discovery.plugin.admincenter.endpoint
}"
)
@Value
(
"${swagger.service.base.package:}"
)
private
String
basePackage
;
private
String
basePackage
;
@Value
(
"${swagger.service.description:Admin Center Restful APIs}"
)
@Value
(
"${swagger.service.description:Admin Center Restful APIs}"
)
...
@@ -58,6 +65,9 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -58,6 +65,9 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
@Value
(
"${swagger.service.contact.email:1394997@qq.com}"
)
@Value
(
"${swagger.service.contact.email:1394997@qq.com}"
)
private
String
contactEmail
;
private
String
contactEmail
;
@Value
(
"${swagger.service.termsOfServiceUrl:http://www.nepxion.com"
)
private
String
termsOfServiceUrl
;
@Value
(
"${swagger.cors.registry.enabled:true}"
)
@Value
(
"${swagger.cors.registry.enabled:true}"
)
private
Boolean
corsRegistryEnabled
;
private
Boolean
corsRegistryEnabled
;
...
@@ -66,7 +76,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -66,7 +76,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
return
new
Docket
(
DocumentationType
.
SWAGGER_2
)
.
apiInfo
(
apiInfo
())
.
apiInfo
(
apiInfo
())
.
select
()
.
select
()
.
apis
(
RequestHandlerSelectors
.
basePackage
(
basePackage
))
// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.
apis
(
SwaggerConfiguration
.
basePackage
(
BASE_PACKAGE
+
(
StringUtils
.
isNotEmpty
(
basePackage
.
trim
())
?
","
+
basePackage
.
trim
()
:
StringUtils
.
EMPTY
)
))
// 扫描该包下的所有需要在Swagger中展示的API,@ApiIgnore注解标注的除外
.
paths
(
PathSelectors
.
any
())
.
paths
(
PathSelectors
.
any
())
.
build
();
.
build
();
}
}
...
@@ -79,6 +89,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -79,6 +89,7 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
.
license
(
license
)
.
license
(
license
)
.
licenseUrl
(
licenseUrl
)
.
licenseUrl
(
licenseUrl
)
.
contact
(
new
Contact
(
contactName
,
contactUrl
,
contactEmail
))
.
contact
(
new
Contact
(
contactName
,
contactUrl
,
contactEmail
))
.
termsOfServiceUrl
(
termsOfServiceUrl
)
.
build
();
.
build
();
}
}
...
@@ -92,4 +103,35 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
...
@@ -92,4 +103,35 @@ public class SwaggerConfiguration implements WebMvcConfigurer {
.
allowedOrigins
(
"*"
);
.
allowedOrigins
(
"*"
);
}
}
}
}
public
static
Predicate
<
RequestHandler
>
basePackage
(
String
basePackage
)
{
return
new
Predicate
<
RequestHandler
>()
{
@Override
public
boolean
apply
(
RequestHandler
input
)
{
return
declaringClass
(
input
).
transform
(
handlerPackage
(
basePackage
)).
or
(
true
);
}
};
}
private
static
Function
<
Class
<?>,
Boolean
>
handlerPackage
(
String
basePackage
)
{
return
new
Function
<
Class
<?>,
Boolean
>()
{
@Override
public
Boolean
apply
(
Class
<?>
input
)
{
String
[]
subPackages
=
basePackage
.
split
(
","
);
for
(
String
subPackage
:
subPackages
)
{
boolean
matched
=
input
.
getPackage
().
getName
().
startsWith
(
subPackage
.
trim
());
if
(
matched
)
{
return
true
;
}
}
return
false
;
}
};
}
@SuppressWarnings
(
"deprecation"
)
private
static
Optional
<?
extends
Class
<?>>
declaringClass
(
RequestHandler
input
)
{
return
Optional
.
fromNullable
(
input
.
declaringClass
());
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment