SpringBoot整合Swagger2代码实例

  

首先遵循SpringBoot的三板斧

  

第一步添加依赖

        & lt; !——SwaggerUI接口文档http://{ip}: {prot}/swagger-ui。html——比;   & lt; dependency>   & lt; groupId> io.springfox   & lt; artifactId> springfox-swagger2   & lt; version> {version} & lt;/version>   & lt;/dependency>   & lt; dependency>   & lt; groupId> io.springfox   & lt; artifactId> springfox-swaggerui   & lt; version> {version} & lt;/version>   & lt;/dependency>      

第二步添加注解

  

@EnableSwagger2//启动SwaggerUI,在启动类或大摇大摆配置类上添加该注解
  

  

第三步写配置

        @ configuration   @EnableSwagger2   公开课SwaggerConfig {      @ bean   公共摘要api () {/*//可以添加多个标题或参数   ParameterBuilder aParameterBuilder=new ParameterBuilder ();   aParameterBuilder//参数类型支持头,饼干、身体、查询等   .parameterType(“标题”)//参数名   . name(“用户令牌”)//默认值   .defaultValue (“t122222”)   .description(“用户登录凭证”)//指定参数值的类型   .modelRef(新ModelRef(“字符串”))//非必需,这里是全局配置   .required (false) .build ();   List,aParameters=new ArrayList<的在();   aParameters.add (aParameterBuilder.build ());   */返回新摘要(DocumentationType.SWAGGER_2)//返回新摘要(DocumentationType.SPRING_WEB)   .apiInfo (apiInfo ())   .pathMapping (“/?   .select()//选择那些路径和api会生成文档   .apis (RequestHandlerSelectors.any())//对所有api进行监控//不显示错误的接口地址   .paths (Predicates.not (PathSelectors.regex("/错误。* ")))//错误错误路径不监控   .paths (Predicates.not (PathSelectors.regex(“/致动器。*”)))//错误错误路径不监控   .paths (PathSelectors.regex(“/*”。))//对根下所有路径进行监控   .paths (PathSelectors.any())//对所有路径进行监控//自行修改为自己的包路径//.apis (RequestHandlerSelectors.basePackage (“com.happyloves.zc.service.account.api”))   .build ()//.globalOperationParameters (aParameters)   .enable(真正的);   }      私人ApiInfo ApiInfo () {   返回新ApiInfoBuilder ()   .title (“API接口”)   .description (“API接口文档”)//服务条款网址//.termsOfServiceUrl (“https://www.google.com”)   .version (“1.0”)//.contact(新联系人(“啦啦啦”,“url”,“电子邮件”))   .build ();   }   }      

扩展:swagger-bootstrap-ui是springfox-swagger的增强UI实现,为Java开发者在使用得意的时候,能拥有一份简洁,强大的接口文档

  

体验项目地址码云:https://gitee.com/xiaoym/swagger-bootstrap-ui

  

GitHub: https://github.com/xiaoymin/Swagger-Bootstrap-UI

  

在线体验:http://swagger-bootstrap-ui.xiaominfo.com/doc.html

  

代码集成示例

  

SpringBoot在线演示地址:https://gitee.com/xiaoym/swagger-bootstrap-ui-demo

  

Spring Mvc在线演示地址:https://gitee.com/xiaoym/swagger-bootstrap-ui-demo/tree/master/swagger-bootstrap-ui-demo-mvc

  

添加依赖

        & lt; !——swagger-bootstrap-ui是大摇大摆的增强UI实现,使文档更友好一点儿http://{ip}: {prot}/医生。html——比;   & lt; dependency>   & lt; groupId> com.github.xiaoymin   & lt; artifactId> swagger-bootstrap-ui   & lt; version> 1.9.6   & lt;/dependency>      

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

SpringBoot整合Swagger2代码实例