好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

手把手教你SpringBoot快速集成Swagger的配置过程

导语

相信大家无论是做前端还是做后端的,都被接口接口文档所折磨过,前端抱怨接口文档和后端给的不一致,后端抱怨写接口文档很麻烦,所以swagger就诞生了。直接配置即可自动生成接口文档,而且提供了高效的api测试
话不多说直接开干
导入springboot集成swagger所需要的依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!--web方便测试-->

    <dependency>

      <groupid>org.springframework.boot</groupid>

      <artifactid>spring-boot-starter-web</artifactid>

    </dependency>

    <!-- swagger2核心包 -->

    <dependency>

      <groupid>io.springfox</groupid>

      <artifactid>springfox-swagger2</artifactid>

      <version> 2.9 . 2 </version>

    </dependency>

    <!-- swagger-ui 可视化界面 -->

    <dependency>

      <groupid>io.springfox</groupid>

      <artifactid>springfox-swagger-ui</artifactid>

      <version> 2.9 . 2 </version>

    </dependency>

swagger可视化界面可分为三个区域

swagger相关配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package com.example.config;

 

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

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;

 

import java.util.arraylist;

 

@configuration

@enableswagger2 //开启swagger的使用

public class swaggerconfig {

 

   @bean   //swagger的使用主要是要将docket对象传入ioc容器

   public docket docket(){

     return new docket(documentationtype.swagger_2)

         .apiinfo(apiinfo()) //关于文档的各种信息

         .enable( true ) //使swagger生效

         .groupname( "常安祖" )

         .select() //选择扫描的接口

         .apis(requesthandlerselectors.basepackage( "com.example.controller" )) //指定扫描的接口

         .build();

   }

 

 

   public apiinfo apiinfo(){

     contact contact = new contact( "长安" , "https://blog.csdn.net/weixin_45647685" , "719801748@qq.com" );//个人的联系方式

     return new apiinfo( "长安的文档" , "长安的开发文档" , "1.0" , "urn:tos" , null , "apache 2.0" , "http://www.apache.org/licenses/license-2.0" , new arraylist());//文档的各种信息

   }

}

@apimodel( ) //主要用来标注返回的实体类
@apimodelproperty( ) //主要用来标注实体类中的属性
案例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

@apimodel ( "用户的实体类" )

public class user implements serializable {

 

   @apimodelproperty ( "用户的id" )

   private integer id;

 

   @apimodelproperty ( "用户的姓名" )

   private string name;

 

   @apimodelproperty ( "用户的年纪" )

   private integer age;

 

   public integer getid() {

     return id;

   }

   public user(integer id, string name, integer age) {

     this .id = id;

     this .name = name;

     this .age = age;

   }

 

   public void setid(integer id) {

     this .id = id;

   }

 

   public string getname() {

     return name;

   }

 

   public void setname(string name) {

     this .name = name;

   }

 

   public integer getage() {

     return age;

   }

 

   public void setage(integer age) {

     this .age = age;

   }

}

@apimodelproperty 用来标注api接口
案例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.yangzihao.controller;

 

import com.yangzihao.entity.user;

import io.swagger.annotations.apimodelproperty;

import org.springframework.stereotype.controller;

import org.springframework.web.bind.annotation.getmapping;

import org.springframework.web.bind.annotation.requestmapping;

 

@restcontroller

public class usercontroller {

 

   @apimodelproperty ( "得到一个user" )

   @getmapping ( "/getuser" )

   public user getuser(){

     return new user( 1 , "测试" , 18 );

   }

}

进入swagger可视化界面

使用swagger进行接口测试

执行

到此这篇关于手把手教你springboot快速集成swagger的配置过程的文章就介绍到这了,更多相关springboot集成swagger内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_45647685/article/details/113850616

查看更多关于手把手教你SpringBoot快速集成Swagger的配置过程的详细内容...

  阅读:11次