Java集成swagger文檔組件
一:簡介
Swagger 是一個規(guī)范和完整的框架,用于生成、描述、調用和可視化 RESTful 風格的 Web 服務。總體目標是使客戶端和文件系統(tǒng)作為服務器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務器端的代碼,允許API來始終保持同步。Swagger 讓部署管理和使用功能強大的API從未如此簡單。
二:集成swagger
1.引入pom.xml文件包(導入4個jar包)
注意:jdk1.8以上才能運行swagger2
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <!--swagger-ui--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> <!--swagger-ui增強--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.4</version> </dependency> <!--swagger-xml bind--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency>
2.要想使用Swagger,必須編寫一個配置類來配置 Swagger,這里的配置類如下
@Configuration @EnableSwagger2 public class SwaggerConfig { private String title = "標題.."; private String description = ""; private String termsOfServiceUrl = ""; private String version = "版本號.."; @Bean public Docket createDefaultRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()) .build().groupName("default").securitySchemes(securitySchemes()).securityContexts(securityContexts()); } @Bean public Docket createTestRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.ant("/test/**")).build().groupName("測試/調試").securitySchemes(securitySchemes()) .securityContexts(securityContexts()); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl) .version(version).build(); } private List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return newArrayList(new SecurityReference("token", authorizationScopes)); } private List<SecurityContext> securityContexts() { return newArrayList(SecurityContext.builder().securityReferences(defaultAuth()) .forPaths(PathSelectors.regex("^(?!auth).*$")).build()); } private List<ApiKey> securitySchemes() { return newArrayList(new ApiKey("token", "token", "header")); } }
3.集成RESTful風格接口示例
@Api(tags = "測試") @RestController public class TestController { @ApiOperation("get方法") @GetMapping("getInfo") public void getInfo(){ } }
4.控制臺打印路徑地址(可選配置)
@Slf4j @SpringBootApplication public class SpringbootApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication.run(SpringbootApplication.class, args); } @Autowired Environment environment; @Override public void run(ApplicationArguments args) throws Exception { log.info("項目已啟動,端口:" + environment.getProperty("local.server.port")); log.info("swagger文檔地址:http://localhost:" + environment.getProperty("local.server.port") + "/swagger-ui.html"); log.info("swagger文檔地址:http://localhost:" + environment.getProperty("local.server.port") + "/doc.html"); } }
三:配置運用swagger
1. http://ip:port/swagger-ui.html
2. http://ip:port/doc.html
到此這篇關于Java集成swagger文檔組件的文章就介紹到這了,更多相關Java集成swagger內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java技巧分享之利用RxJava打造可觀測數(shù)據(jù)RxLiveData
這篇文章主要來和大家分享一個Java技巧,那就是利用RxJava打造可觀測數(shù)據(jù)RxLiveData,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-06-06java組件commons-fileupload實現(xiàn)文件上傳、下載、在線打開
這篇文章主要介紹了java組件commons-fileupload實現(xiàn)文件上傳、下載、在線打開,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10Spring注解驅動擴展原理BeanFactoryPostProcessor
這篇文章主要介紹了Spring注解驅動擴展原理BeanFactoryPostProcessor,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03SpringCloudAlibaba整合Feign實現(xiàn)遠程HTTP調用的簡單示例
這篇文章主要介紹了SpringCloudAlibaba 整合 Feign 實現(xiàn)遠程 HTTP 調用,文章中使用的是OpenFeign,是Spring社區(qū)開發(fā)的組件,需要的朋友可以參考下2021-09-09