|
@@ -1,6 +1,12 @@
|
|
|
package com.txz.mall.configurer;
|
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import io.swagger.v3.oas.models.Components;
|
|
|
+import io.swagger.v3.oas.models.OpenAPI;
|
|
|
+import io.swagger.v3.oas.models.info.Contact;
|
|
|
+import io.swagger.v3.oas.models.info.Info;
|
|
|
+import io.swagger.v3.oas.models.info.License;
|
|
|
+import io.swagger.v3.oas.models.parameters.Parameter;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Profile;
|
|
@@ -10,8 +16,6 @@ import springfox.documentation.builders.PathSelectors;
|
|
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
import springfox.documentation.schema.ModelRef;
|
|
|
import springfox.documentation.service.ApiInfo;
|
|
|
-import springfox.documentation.service.Contact;
|
|
|
-import springfox.documentation.service.Parameter;
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
@@ -23,43 +27,33 @@ import java.util.List;
|
|
|
* @author Fcx
|
|
|
*/
|
|
|
@Configuration
|
|
|
-@EnableSwagger2
|
|
|
-@Profile({"dev","test"})
|
|
|
+@Profile({"dev", "test"})
|
|
|
public class SwaggerConfig {
|
|
|
-
|
|
|
+
|
|
|
@Bean
|
|
|
- public Docket docket(){
|
|
|
- //添加header参数
|
|
|
- List<Parameter> pars = new ArrayList<>();
|
|
|
- ParameterBuilder ticketPar = new ParameterBuilder();
|
|
|
- ticketPar.name("token").description("user token")
|
|
|
- .modelRef(new ModelRef("string")).parameterType("header")
|
|
|
- .required(false).build(); //header中的ticket参数非必填,传空也可以
|
|
|
- ticketPar = new ParameterBuilder();
|
|
|
- ticketPar.name("appCode").description("appCode")
|
|
|
- .modelRef(new ModelRef("string")).parameterType("header")
|
|
|
- .required(false).build(); //header中的ticket参数非必填,传空也可以
|
|
|
-
|
|
|
- pars.add(ticketPar.build());
|
|
|
- return new Docket(DocumentationType.SWAGGER_2)
|
|
|
- .globalOperationParameters(pars)
|
|
|
- .groupName("demo")
|
|
|
- .apiInfo(getApiInfo())
|
|
|
- .select()
|
|
|
- //设置basePackage会将包下的所有被@Api标记类的所有方法作为api
|
|
|
- .apis(RequestHandlerSelectors.basePackage("com.txz.mall.controller"))
|
|
|
- //只有标记了@ApiOperation的方法才会暴露出给swagger
|
|
|
- .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
|
|
- .paths(PathSelectors.regex("/api/.*")).build();
|
|
|
+ public OpenAPI customOpenAPI() {
|
|
|
+ return new OpenAPI()
|
|
|
+ .components(new Components()
|
|
|
+ .addParameters("token", new io.swagger.v3.oas.models.parameters.Parameter()
|
|
|
+ .name("token")
|
|
|
+ .description("user token")
|
|
|
+ .required(false)
|
|
|
+ .in("header"))
|
|
|
+ .addParameters("appCode", new Parameter()
|
|
|
+ .name("appCode")
|
|
|
+ .description("appCode")
|
|
|
+ .required(false)
|
|
|
+ .in("header")))
|
|
|
+ .info(new Info()
|
|
|
+ .title("API接口文档")
|
|
|
+ .description("swagger2 demo api")
|
|
|
+ .version("1.0")
|
|
|
+ .contact(new Contact()
|
|
|
+ .name("admin")
|
|
|
+ .url("http://localhost/swagger-ui.html")
|
|
|
+ .email("xxx@qq.com"))
|
|
|
+ .license(new License()
|
|
|
+ .name("Apache 2.0")
|
|
|
+ .url("http://localhost/swagger-ui.html")));
|
|
|
}
|
|
|
-
|
|
|
- private ApiInfo getApiInfo(){
|
|
|
- return new ApiInfoBuilder()
|
|
|
- .title("API接口文档")
|
|
|
- .description("swagger2 demo api")
|
|
|
- .termsOfServiceUrl("http://localhost/swagger-ui.html")
|
|
|
- .version("1.0").contact(new Contact("admin", "http://localhost/swagger-ui.html", "xxx@qq.com"))
|
|
|
- .build();
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+}
|