spring boot 整合swagger-ui 与 swagger2

首先新建一个spring boot 工程: 我直接在以前的仓库里写了,application如下:

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

添加maven依赖:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>

完整pom.xml

<?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>spring-boot</groupId>
    <artifactId>spring-study</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.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-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--spring-boot-starter-test 起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>LATEST</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.29</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

添加swagger2配置:

//加入配置注解
@Configuration
//开启swagger
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("demo"))//这里是项目的基本包,改成项目对应的包路径
                .paths(PathSelectors.regex("(?!/error.*).*")).build();
    }

    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("天王盖地虎api接口").description("说明RESTful APIs").contact("swagger@gmail.com").version("2018.0")
                .build();
    }
}

然后建一个相应的controller

对应的bean:

package demo.springboottest;

public class Bird {
    String name;
    String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

接口:

package demo.springboottest.service;

public interface Ibird {

    String run();

    String sayhello();
}

接口实现:

package demo.springboottest.controller;

import demo.springboottest.Bird;
import demo.springboottest.service.Ibird;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/bird")
public class BirdController {

    @Autowired
    private Ibird ibird;

    @GetMapping(value = "/run")
    public String run() {
        return ibird.run();
    }

    @PostMapping(value = "/hello")
    public String sayHello() {
        return ibird.sayhello();
    }

    @GetMapping(value = "/runparam")
    public String runwithparam(@RequestParam(value = "param") String param) {
        System.out.println("param is :" + param);
        return ibird.run();
    }

    @PostMapping(value = "/helloparam")
    public String sayHellowithparam(@RequestBody Bird bird) {

        System.out.println("param is : " + bird);
        return ibird.sayhello();
    }
}

controller:

package demo.springboottest.controller;

import demo.springboottest.Bird;
import demo.springboottest.service.Ibird;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/bird")
public class BirdController {

    @Autowired
    private Ibird ibird;

    @GetMapping(value = "/run")
    public String run() {
        return ibird.run();
    }

    @PostMapping(value = "/hello")
    public String sayHello() {
        return ibird.sayhello();
    }

    @GetMapping(value = "/runparam")
    public String runwithparam(@RequestParam(value = "param") String param) {
        System.out.println("param is :" + param);
        return ibird.run();
    }

    @PostMapping(value = "/helloparam")
    public String sayHellowithparam(@RequestBody Bird bird) {

        System.out.println("param is : " + bird);
        return ibird.sayhello();
    }
}

启动工程,浏览器输入:http://127.0.0.1:8080/swagger-ui.html#/

更多参考官网:https://swagger.io/

源码直接更新在练习spring boot 的项目里了:

https://github.com/xiaomingtongxie/springboot-study.git

代码已更新,对你有帮助的话,小哥哥小姐姐,帮点下赞哇

应评论小哥哥要求,将json/doc 转静态.html或pdf 别人写的demo已跑通,效果图:

demo地址:

https://github.com/Swagger2Markup/spring-swagger2markup-demo

待我研究一下,再更到博客里哇。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏VRPinea

新CG新武器,《源震》等你来玩!

35510
来自专栏镁客网

「测评」为信仰充值的时候到了——猫王2蓝牙收音机测评

1594
来自专栏VRPinea

五款鬼怪类VR游戏,陪你共度清明小长假(文章高能)

3207
来自专栏腾讯高校合作

深圳大学校长李清泉谈地方高校探索高校改革

近日深圳大学校长李清泉做客人民网百名校长访谈录节目。与网友们一起交流深圳大学在高校体制改革中的新使命和新举措。 采访原文链接:http://edu.people...

3476
来自专栏顶级程序员

小猪佩奇到底有多火?数据和事实告诉你……

豆瓣评分高达9.2,素来挑剔的豆瓣用户可以给这样一部学龄前儿童动画片这么高的评分,足以表明用户对它的认可,说明其质量绝对过硬,要知道此前大火的《熊出没》、《喜洋...

882
来自专栏星流全栈

RethinkDB is shutting down

1105
来自专栏理论坞

2017英国封面设计学院奖获奖作品,能学几招是几招

从 2014 年开始,书籍封面设计师 Jon Gary 和 Jamie Keenan 开始举办英国封面设计学院奖(The Academy of British ...

993
来自专栏BestSDK

网警揭秘信息泄露:身份证手机号最便宜,一两百元可买上亿条

8月19日,即将成为大学新生的山东临沂18岁女孩徐玉玉因为接了一个陌生电话,家人为她东拼西凑的9900元学费被骗光。在从派出所报案回家的路上,女...

2917
来自专栏区块之恋

EOS暴涨40%+之后1.2.3.4.

1474
来自专栏大数据文摘

只有CEO才玩得起的奢侈运动

3248

扫码关注云+社区