|
12 | 12 | import org.springframework.context.annotation.Bean; |
13 | 13 | import org.springframework.context.annotation.Configuration; |
14 | 14 | import org.springframework.context.annotation.Import; |
15 | | -import springfox.documentation.builders.ApiInfoBuilder; |
16 | | -import springfox.documentation.builders.ParameterBuilder; |
17 | | -import springfox.documentation.builders.PathSelectors; |
18 | | -import springfox.documentation.builders.RequestHandlerSelectors; |
| 15 | +import org.springframework.util.StringUtils; |
| 16 | +import springfox.documentation.builders.*; |
19 | 17 | import springfox.documentation.schema.ModelRef; |
20 | 18 | import springfox.documentation.service.ApiInfo; |
21 | 19 | import springfox.documentation.service.Contact; |
22 | 20 | import springfox.documentation.service.Parameter; |
| 21 | +import springfox.documentation.service.ResponseMessage; |
23 | 22 | import springfox.documentation.spi.DocumentationType; |
| 23 | +import org.springframework.web.bind.annotation.RequestMethod; |
24 | 24 | import springfox.documentation.spring.web.plugins.Docket; |
25 | 25 |
|
26 | 26 | import java.util.*; |
@@ -82,21 +82,37 @@ public List<Docket> createRestApi(SwaggerProperties swaggerProperties) { |
82 | 82 | excludePath.add(PathSelectors.ant(path)); |
83 | 83 | } |
84 | 84 |
|
85 | | - Docket docket = new Docket(DocumentationType.SWAGGER_2) |
| 85 | + Docket docketForBuilder = new Docket(DocumentationType.SWAGGER_2) |
86 | 86 | .host(swaggerProperties.getHost()) |
87 | 87 | .apiInfo(apiInfo) |
88 | 88 | .globalOperationParameters(buildGlobalOperationParametersFromSwaggerProperties( |
89 | | - swaggerProperties.getGlobalOperationParameters())) |
90 | | - .select() |
| 89 | + swaggerProperties.getGlobalOperationParameters())); |
| 90 | + |
| 91 | + // 全局响应消息 |
| 92 | + if (!swaggerProperties.getApplyDefaultResponseMessages()) { |
| 93 | + |
| 94 | + ResponseMessageBuilder responseMessageBuilder401 = new ResponseMessageBuilder() |
| 95 | + .code(401) |
| 96 | + .message(swaggerProperties.getGlobalResponseMessage().getMessage401()); |
| 97 | + if (!StringUtils.isEmpty(swaggerProperties.getGlobalResponseMessage().getModelRef401())) |
| 98 | + responseMessageBuilder401.responseModel( |
| 99 | + new ModelRef(swaggerProperties.getGlobalResponseMessage().getModelRef401())); |
| 100 | + |
| 101 | + |
| 102 | + List<ResponseMessage> responseMessages = new ArrayList(); |
| 103 | + responseMessages.add(responseMessageBuilder401.build()); |
| 104 | + docketForBuilder.useDefaultResponseMessages(swaggerProperties.getApplyDefaultResponseMessages()) |
| 105 | + .globalResponseMessage(RequestMethod.GET,responseMessages); |
| 106 | + } |
| 107 | + |
| 108 | + Docket docket = docketForBuilder.select() |
91 | 109 | .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())) |
92 | 110 | .paths( |
93 | 111 | Predicates.and( |
94 | 112 | Predicates.not(Predicates.or(excludePath)), |
95 | 113 | Predicates.or(basePath) |
96 | 114 | ) |
97 | | - ) |
98 | | - .build(); |
99 | | - |
| 115 | + ).build(); |
100 | 116 | configurableBeanFactory.registerSingleton("defaultDocket", docket); |
101 | 117 | docketList.add(docket); |
102 | 118 | return docketList; |
|
0 commit comments