1.pom.xml 添加依赖
<!--swagger2 依赖--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency>
2.配置一个配置类
package cn.cenxi.express.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration //@EnableSwagger2 [如果在这里配置的话,则不需要在启动类配置了] public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()).build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("校园快递代拿api") .description("校园快递代拿后端应用接口") .version("1.0") .build(); } }View Code
3.在启动类开启
@EnableSwagger2
4.测试
启动工程后 ,
使用本地工程地址和端口 加 /swagger-ui.html
如我的 http://localhost:57/swagger-ui.html
5.注解使用如下