swagger是什麼?
swagger是一個後臺人員專門測試的一個簡單工具,特別適合做前後端分離的項目,之前我們一直都是用的postman,但是這個有點複雜,路徑需要自己,而swagger則直接使用,接下來我們就直接上代碼說一說swagger,文采不好往見諒!
1.首先需要兩個jar包
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2.配置swagger核心類
@Configuration public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.pcitc.scl.business.productoil.service.controller")) //自己的包名 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springboot利用swagger構建api文檔") .description("簡單優雅的restfun風格,http://blog.csdn.net/saytime") .termsOfServiceUrl("http://blog.csdn.net/saytime") .version("1.0") .build(); } }
3.springApppplication.java springBoot核心類中的配置
主要是在類上加註解@EnableSwagger2
最後直接
http://localhost:8080/swagger-ui.html
就可以看到自己的項目了,如果項目名稱出不來或者項目啓動不起來,請關注我的另一篇文章