Springboot Swagger-ui

一、简介

swagger是一款流行的API框架,用于生成服务文档,可视化、可调用、自定义规范,简化了开发过程,实时同步API文档的强大功能,相对于其余手写API文有极大优点html

二、使用

  • 依赖 版本不一样,自行选择spring

    <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>
  • 配置 1api

    @Configuration
      @EnableSwagger2
      public class Swagger {
    
      	[@Bean](https://my.oschina.net/bean)
      	public Docket createRestApi() {
      		return new Docket(DocumentationType.SWAGGER_2)
      				.apiInfo(apiInfo())
      				.groupName("分组")
      				.select()
      				.apis(RequestHandlerSelectors.basePackage("com.test.controller"))
      				.paths(PathSelectors.any())
      				.build();
      	}
    
      	private ApiInfo apiInfo() {
      		return new ApiInfoBuilder()
      				.title("Spring boot Swagger-ui")
      				.description("描述信息")
      				.termsOfServiceUrl("http://www.baidu.com/")
      				.version("1.0")
      				.build();
      	}
      }
  • 配置 2app

    @EnableSwagger2
      @Configuration
      public class Swagger {
      	@Bean
      	public Docket swaggerApi() {
      		return new Docket(DocumentationType.SWAGGER_2)
      				.groupName("/hello")
      				.genericModelSubstitutes(DeferredResult.class)
      				.useDefaultResponseMessages(false)
      				.forCodeGeneration(true)
      				.pathMapping("")// api测试请求地址,最终调用接口后会和paths拼接在一块儿
      				.select()
      				.paths(PathSelectors.regex("/.*"))// 过滤的接口
      				.build();
      	}
      }
  • 页面 访问 http://localhost:8080/swagger-ui.html 框架

相关文章
相关标签/搜索