SpringBoot集成swagger的实例代码

2025-05-29 0 33

swagger 是一款restful接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法和一些常见问题。如果想深入分析项目源码,了解更多内容,见参考资料。

swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 restful 风格的 web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许api来始终保持同步。swagger 让部署管理和使用功能强大的api从未如此简单。

对于搬砖的同学来说,写接口容易,写接口文档很烦,接口变动,维护接口文档就更更更烦,所以经常能发现文档与程序不匹配。

等过一段时间就连开发者也蒙圈了

swagger2快速方便的解决了以上问题。一个能与spring mvc程序配合组织出强大restful api文档的新宠儿。

下面直接上代码

pom.xml

?

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

42

43
<?xml version="1.0" encoding="utf-8"?>

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelversion>4.0.0</modelversion>

<groupid>com.zhongxin.wealth</groupid>

<artifactid>wealthweb</artifactid>

<version>0.0.1-snapshot</version>

<packaging>jar</packaging>

<name>wealthweb</name>

<description>demo project for spring boot</description>

<parent>

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

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

<version>1.5.9.release</version>

<relativepath/> <!-- lookup parent from repository -->

</parent>

<properties>

<project.build.sourceencoding>utf-8</project.build.sourceencoding>

<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

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

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

</dependency>

<dependency>

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

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

<scope>test</scope>

</dependency>

<dependency>

<groupid>io.springfox</groupid>

<artifactid>springfox-swagger2</artifactid>

<version>2.7.0</version>

</dependency>

<dependency>

<groupid>io.springfox</groupid>

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

<version>2.7.0</version>

</dependency>

</dependencies>

</project>

  创建配置类

?

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
package com.zhongxin.wealth.apiconfig;

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.spi.documentationtype;

import springfox.documentation.spring.web.plugins.docket;

import springfox.documentation.swagger2.annotations.enableswagger2;

/**

* created by dingys on 2017/12/8.

*/

@configuration

@enableswagger2

public class swagger2 {

@bean

public docket createrestapi() {

return new docket(documentationtype.swagger_2)

.apiinfo(apiinfo())

.select()

.apis(requesthandlerselectors.basepackage("com.zhongxin.wealth.web"))

.paths(pathselectors.any())

.build();

}

private apiinfo apiinfo() {

return new apiinfobuilder()

.title("廊坊委贷大数据统计结果输出接口")

.version("1.0")

.build();

}

}

  controller编写

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
package com.zhongxin.wealth.web;

import io.swagger.annotations.apioperation;

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

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

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

/**

* created by dingys on 2017/12/7.

*/

@restcontroller

@requestmapping("/hello")

public class hellowordcontroller {

@apioperation(value="测试接口", notes="这只是一个测试controller调用的接口,没有任何的业务逻辑")

@requestmapping(value = {"/test"},method = requestmethod.get)

public string testhello(){

return "hello";

}

}

  代码完成,准备看效果

SpringBoot集成swagger的实例代码

点击try it out!

SpringBoot集成swagger的实例代码

是不是很详细,很高大上。

注:集成过程中刚开始用的swagger2.2.2版本,会在首页出现一个error的错误提醒

?

1
{“schemavalidationmessages”:[{“level”:”error”,”message”:”can't read from file http://127.0.0.1:8888/v2/api-docs"}]}

  但是浏览器访问:http://127.0.0.1:8888/v2/api-docs 又能获取 结果

?

1
{“swagger”:”2.0”,”info”:{“version”:”1.0”,”title”:”廊坊委贷大数据统计结果输出接口”,”contact”:{},”license”:{}},”host”:”127.0.0.1:8888”,”basepath”:”/“,”tags”:[{“name”:”hello-word-controller”,”description”:”hello word controller”}],”paths”:{“/hello/test”:{“get”:{“tags”:[“hello-word-controller”],”summary”:”测试接口”,”description”:”这只是一个测试controller调用的接口,没有任何的业务逻辑”,”operationid”:”testhellousingget”,”consumes”:[“application/json”],”produces”:[“/“],”responses”:{“200”:{“description”:”ok”,”schema”:{“type”:”string”}},”401”:{“description”:”unauthorized”},”403”:{“description”:”forbidden”},”404”:{“description”:”not found”}}}}}}

  具体原因本人不明,换成2.7.0版本以后没在出现。

总结

以上所述是小编给大家介绍的springboot集成swagger的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://www.cnblogs.com/Smilence1024/p/8004814.html?utm_source=tuicool&utm_medium=referral

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot集成swagger的实例代码 https://www.kuaiidc.com/113734.html

相关文章

发表评论
暂无评论