添加 POM 依赖,最新版可以在 maven 仓库中找到
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
新建一个 swagger 的配置文件
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.ahianzhang.controller")) .paths(PathSelectors.any()) .build(); } /** * @return new ApiInfoBuilder() */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot 自定义 Swagger2") .description("欢迎关注 Hacpai") .termsOfServiceUrl("https://www.hacpai.com")//这个链接访问会报 404 ERROR 不知道是怎么回事 .contact(new Contact("Ahian","http://www.ahianzhang.com", "ahianzhang@gmail.com")) .version("1.0.0") .build(); } }
具体的方法属性可以直接查看源代码,写的比较清楚。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于