欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot集成Swagger2构建在线API文档的代码详解

程序员文章站 2022-09-02 10:13:44
第一部分:代码集成pom.xml io.springfox

第一部分:代码集成

pom.xml

<!--swagger2配置-->
    <dependency>
      <groupid>io.springfox</groupid>
      <artifactid>springfox-swagger-ui</artifactid>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupid>io.springfox</groupid>
      <artifactid>springfox-swagger2</artifactid>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupid>com.github.xiaoymin</groupid>
      <artifactid>swagger-bootstrap-ui</artifactid>
      <version>1.6</version>
    </dependency>

swagger2配置类

package com.liud.demo.config;

import io.swagger.annotations.apioperation;
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.service.contact;
import springfox.documentation.spi.documentationtype;
import springfox.documentation.spring.web.plugins.docket;
import springfox.documentation.swagger2.annotations.enableswagger2;

/**
 * todo
 * swagger2配置类
 * @author liud
 * @version 1.0
 */

@configuration
@enableswagger2
public class swagger2 {
  //配置swagger2核心配置
  @bean
  public docket createrestapi(){
    return new docket(documentationtype.swagger_2) //指定api类型位swagger2
        .apiinfo(apiinfo())            //用于定义api文档汇总信息
        .select()
        //.apis(requesthandlerselectors.basepackage("com.liud.demo.controller")) //指定生成文档的controller
        //.apis(requesthandlerselectors.any()) //为任何接口生成api文档
        //.apis(requesthandlerselectors.withclassannotation(api.class)) //为有@api注解的controller生成api文档
        .apis(requesthandlerselectors.withmethodannotation(apioperation.class)) //为有@apioperation注解的方法生成api文档
        .paths(pathselectors.any())
        .build();
  }

  //api基本信息
  private apiinfo apiinfo(){
    return new apiinfobuilder()
        .title("springbootdemo的项目接口api") //文档标题
        .contact(new contact("liud", //作者
            "",
            "")) //联系人
        .description("springbootdemo的项目接口api")//详细信息
        .version("1.0.0")//文档版本号
        .termsofserviceurl("")//网站地址
        .build();
  }
}

controller

package com.liud.demo.controller;

import com.liud.demo.service.helloservice;
import io.swagger.annotations.api;
import io.swagger.annotations.apioperation;
import io.swagger.annotations.apiparam;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.httpservletrequest;

/**
 * todo
 *
 * @author liud
 * @version 1.0
 */
@restcontroller
@api(tags = {"hello操作接口"})
public class hellocontroller {
  @apioperation(value = "根据用户名获取用户信息接口")
  @requestmapping(value = "/getuserinfo",method = requestmethod.post)
  public string getuserinfo(httpservletrequest request,
               @apiparam(name="username",value = "用户名",required = true) string username){
    return "输入的姓名:"+username+",这个用户的信息已经存在!";
  }
}

第二部分 使用 ①原路径模式

在浏览器上输入url:
http://{ip}:{port}/swagger-ui.html#/

我的地址:http://127.0.0.1:8081/swagger-ui.html

SpringBoot集成Swagger2构建在线API文档的代码详解

②文档模式

在浏览器上输入url:
http://{ip}:{port}/doc.html

我的地址:http://127.0.0.1:8081/doc.html

SpringBoot集成Swagger2构建在线API文档的代码详解

第三部分 swagger2常用注解

常用注解:

@api()用于类;
表示标识这个类是swagger的资源
tags–表示说明
value–也是说明,可以使用tags替代
但是tags如果有多个值,会生成多个list

SpringBoot集成Swagger2构建在线API文档的代码详解

效果:

SpringBoot集成Swagger2构建在线API文档的代码详解

@apioperation()用于方法;
表示一个http请求的操作
value用于方法描述
notes用于提示内容
tags可以重新分组(视情况而用)

@apiparam()用于方法,参数,字段说明;
表示对参数的添加元数据(说明或是否必填等)
name–参数名
value–参数说明
required–是否必填

@apiparam(name="username",value = "用户名",required = true) string username

效果:

SpringBoot集成Swagger2构建在线API文档的代码详解

  • @apimodel()用于类
  • 表示对类进行说明,用于参数用实体类接收
  • @apimodelproperty()用于方法,字段
  • 表示对model属性的说明或者数据操作更改
  • @apiignore()用于类,方法,方法参数
  • 表示这个方法或者类被忽略
  • @apiimplicitparam() 用于方法
  • 表示单独的请求参数
  • @apiimplicitparams() 用于方法,包含多个@apiimplicitparam

到此这篇关于springboot集成swagger2构建在线api文档的文章就介绍到这了,更多相关springboot集成swagger2内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!