Swagger是基于Docker的,所以下面的配置全是Docker相关
package com.example.swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } //配置页面的一些相关介绍 @Bean public ApiInfo apiInfo(){//通过源码发现只有构造方法并没有set方法,所以只有构造方法能够注入 Contact contact = new Contact("作者名字","跟多相关链接","邮箱");//作者信息 ApiInfo apiInfo = new ApiInfo( "api文档的标题", "描述信息","版本号","服务条款的链接", contact,"许可证", "许可证的链接", new ArrayList() ); return apiInfo; } }
RequestHandlerSelectors有几种扫描方式:
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.none())
.apis(withClassAnnotation.)
.apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class))
package com.example.swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .select() /*RequestHandlerSelectors有几种扫描方式: 1.basePackage:指定包扫描 示例:.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller")) 2.any:扫描该项目的所有请求链接 示例:.apis(RequestHandlerSelectors.any()) 3.none:所有请求链接都不扫描 示例:.apis(RequestHandlerSelectors.none()) 4.withClassAnnotation:通过类的注解扫描,就是类名上面的注释 可以是Controller,RestController,RequestMapping,GetMapping ,PostMapping等等 示例:.apis(withClassAnnotation.) 5.withMethodAnnotation:通过方法的注解扫描,也就是方法上面的注解 可以是RequestMapping ,GetMapping ,PostMapping等等 示例:.apis(RequestHandlerSelectors.withMethodAnnotation(RequestMapping.class)) 一般推荐第一种,其他作为了解即可 */ .apis(RequestHandlerSelectors.withClassAnnotation(Controller.class)) .build(); } //配置页面的一些相关介绍 @Bean public ApiInfo apiInfo(){//通过源码发现只有构造方法并没有set方法,所以只有构造方法能够注入 Contact contact = new Contact("作者名字","跟多相关链接","邮箱");//作者信息 ApiInfo apiInfo = new ApiInfo( "api文档的标题", "描述信息","版本号","服务条款的链接", contact,"许可证", "许可证的链接", new ArrayList() ); return apiInfo; } }
下一个:南平领养猫(泉州领养猫)