22
33import com .google .common .base .Predicate ;
44import com .google .common .base .Predicates ;
5+ import org .springframework .beans .BeansException ;
6+ import org .springframework .beans .factory .BeanFactory ;
7+ import org .springframework .beans .factory .BeanFactoryAware ;
8+ import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
59import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
610import org .springframework .context .annotation .Bean ;
711import org .springframework .context .annotation .Configuration ;
1418import springfox .documentation .spring .web .plugins .Docket ;
1519
1620import java .util .ArrayList ;
21+ import java .util .LinkedList ;
1722import java .util .List ;
1823
1924/**
2227 * My blog: http://blog.didispace.com
2328 */
2429@ Configuration
25- public class SwaggerAutoConfiguration {
30+ public class SwaggerAutoConfiguration implements BeanFactoryAware {
31+
32+ private BeanFactory beanFactory ;
2633
2734 @ Bean
2835 @ ConditionalOnMissingBean
@@ -32,46 +39,113 @@ public SwaggerProperties swaggerProperties() {
3239
3340 @ Bean
3441 @ ConditionalOnMissingBean
35- public Docket createRestApi (SwaggerProperties swaggerProperties ) {
36- ApiInfo apiInfo = new ApiInfoBuilder ()
37- .title (swaggerProperties .getTitle ())
38- .description (swaggerProperties .getDescription ())
39- .version (swaggerProperties .getVersion ())
40- .license (swaggerProperties .getLicense ())
41- .licenseUrl (swaggerProperties .getLicenseUrl ())
42- .contact (new Contact (swaggerProperties .getContact ().getName (),
43- swaggerProperties .getContact ().getUrl (),
44- swaggerProperties .getContact ().getEmail ()))
45- .termsOfServiceUrl (swaggerProperties .getTermsOfServiceUrl ())
46- .build ();
47-
48- // base-path处理
49- // 当没有配置任何path的时候,解析/**
50- if (swaggerProperties .getBasePath ().isEmpty ()) {
51- swaggerProperties .getBasePath ().add ("/**" );
52- }
53- List <Predicate <String >> basePath = new ArrayList ();
54- for (String path : swaggerProperties .getBasePath ()) {
55- basePath .add (PathSelectors .ant (path ));
56- }
42+ public List <Docket > createRestApi (SwaggerProperties swaggerProperties ) {
43+ ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory ) beanFactory ;
44+
45+ // 没有分组
46+ if (swaggerProperties .getDocket ().size () == 0 ) {
47+ ApiInfo apiInfo = new ApiInfoBuilder ()
48+ .title (swaggerProperties .getTitle ())
49+ .description (swaggerProperties .getDescription ())
50+ .version (swaggerProperties .getVersion ())
51+ .license (swaggerProperties .getLicense ())
52+ .licenseUrl (swaggerProperties .getLicenseUrl ())
53+ .contact (new Contact (swaggerProperties .getContact ().getName (),
54+ swaggerProperties .getContact ().getUrl (),
55+ swaggerProperties .getContact ().getEmail ()))
56+ .termsOfServiceUrl (swaggerProperties .getTermsOfServiceUrl ())
57+ .build ();
58+
59+ // base-path处理
60+ // 当没有配置任何path的时候,解析/**
61+ if (swaggerProperties .getBasePath ().isEmpty ()) {
62+ swaggerProperties .getBasePath ().add ("/**" );
63+ }
64+ List <Predicate <String >> basePath = new ArrayList ();
65+ for (String path : swaggerProperties .getBasePath ()) {
66+ basePath .add (PathSelectors .ant (path ));
67+ }
68+
69+ // exclude-path处理
70+ List <Predicate <String >> excludePath = new ArrayList ();
71+ for (String path : swaggerProperties .getExcludePath ()) {
72+ excludePath .add (PathSelectors .ant (path ));
73+ }
74+
75+ Docket docket = new Docket (DocumentationType .SWAGGER_2 )
76+ .apiInfo (apiInfo )
77+ .select ()
78+ .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
79+ .paths (
80+ Predicates .and (
81+ Predicates .not (Predicates .or (excludePath )),
82+ Predicates .or (basePath )
83+ )
84+ )
85+ .build ();
5786
58- // exclude-path处理
59- List <Predicate <String >> excludePath = new ArrayList ();
60- for (String path : swaggerProperties .getExcludePath ()) {
61- excludePath .add (PathSelectors .ant (path ));
87+ configurableBeanFactory .registerSingleton ("defaultDocket" , docket );
88+ return null ;
6289 }
6390
64- return new Docket (DocumentationType .SWAGGER_2 )
65- .apiInfo (apiInfo )
66- .select ()
67- .apis (RequestHandlerSelectors .basePackage (swaggerProperties .getBasePackage ()))
68- .paths (
69- Predicates .and (
70- Predicates .not (Predicates .or (excludePath )),
71- Predicates .or (basePath )
72- )
73- )
74- .build ();
91+ // 分组创建
92+ List <Docket > docketList = new LinkedList <>();
93+ for (String groupName : swaggerProperties .getDocket ().keySet ()) {
94+ SwaggerProperties .DocketInfo docketInfo = swaggerProperties .getDocket ().get (groupName );
95+
96+ ApiInfo apiInfo = new ApiInfoBuilder ()
97+ .title (docketInfo .getTitle ().isEmpty () ? swaggerProperties .getTitle () : docketInfo .getTitle ())
98+ .description (docketInfo .getDescription ().isEmpty () ? swaggerProperties .getDescription () : docketInfo .getDescription ())
99+ .version (docketInfo .getVersion ().isEmpty () ? swaggerProperties .getVersion () : docketInfo .getVersion ())
100+ .license (docketInfo .getLicense ().isEmpty () ? swaggerProperties .getLicense () : docketInfo .getLicense ())
101+ .licenseUrl (docketInfo .getLicenseUrl ().isEmpty () ? swaggerProperties .getLicenseUrl () : docketInfo .getLicenseUrl ())
102+ .contact (
103+ new Contact (
104+ docketInfo .getContact ().getName ().isEmpty () ? swaggerProperties .getContact ().getName () : docketInfo .getContact ().getName (),
105+ docketInfo .getContact ().getUrl ().isEmpty () ? swaggerProperties .getContact ().getUrl () : docketInfo .getContact ().getUrl (),
106+ docketInfo .getContact ().getEmail ().isEmpty () ? swaggerProperties .getContact ().getEmail () : docketInfo .getContact ().getEmail ()
107+ )
108+ )
109+ .termsOfServiceUrl (docketInfo .getTermsOfServiceUrl ().isEmpty () ? swaggerProperties .getTermsOfServiceUrl () : docketInfo .getTermsOfServiceUrl ())
110+ .build ();
111+
112+ // base-path处理
113+ // 当没有配置任何path的时候,解析/**
114+ if (docketInfo .getBasePath ().isEmpty ()) {
115+ docketInfo .getBasePath ().add ("/**" );
116+ }
117+ List <Predicate <String >> basePath = new ArrayList ();
118+ for (String path : docketInfo .getBasePath ()) {
119+ basePath .add (PathSelectors .ant (path ));
120+ }
121+
122+ // exclude-path处理
123+ List <Predicate <String >> excludePath = new ArrayList ();
124+ for (String path : docketInfo .getExcludePath ()) {
125+ excludePath .add (PathSelectors .ant (path ));
126+ }
127+
128+ Docket docket = new Docket (DocumentationType .SWAGGER_2 )
129+ .apiInfo (apiInfo )
130+ .groupName (groupName )
131+ .select ()
132+ .apis (RequestHandlerSelectors .basePackage (docketInfo .getBasePackage ()))
133+ .paths (
134+ Predicates .and (
135+ Predicates .not (Predicates .or (excludePath )),
136+ Predicates .or (basePath )
137+ )
138+ )
139+ .build ();
140+
141+ configurableBeanFactory .registerSingleton (groupName , docket );
142+ docketList .add (docket );
143+ }
144+ return docketList ;
75145 }
76146
147+ @ Override
148+ public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
149+ this .beanFactory = beanFactory ;
150+ }
77151}
0 commit comments