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 条评论
登录 后参与评论

相关文章

来自专栏一个会写诗的程序员的博客

React.js 集成 Kotlin Spring Boot 开发 Web 应用实例详解工程源代码参考文章

React.js 集成 Kotlin Spring Boot 开发 Web 应用实例详解

1272
来自专栏杂烩

springmvc @ResponseBody相关配置

1681
来自专栏算法修养

CodeForces 665A Buses Between Cities

A. Buses Between Cities time limit per test 1 second memory limit per test ...

2954
来自专栏三木的博客

基于Jenkins和Koji的代码管理及构建系统

<h1>Summary</h1> Currently, we KVM team are maintaining the code repository of t...

2227
来自专栏cloudskyme

Could not find artifact com.sun:tools:jar:1.5.0

【maven package】,则依然报错,但报的是另外一个错误: [INFO] Scanning for projects…[INFO]           ...

3054
来自专栏后端之路

tomcat 生产环境crash

最近生产系统erp不太稳定,时不时出现tomcat crash,并且log下面没有留下任何证据。 收到邮件如下 告警主机:f6_web0 告警时间:2016.1...

1987
来自专栏算法修养

ZOJ 3927 Programming Ability Test

Programming Ability Test (PAT) aims to evaluate objectively, through unified exa...

2494
来自专栏李成熙heyli

Deploy Using Travis-CI And Github Webhook — webpack doc as an example

Overview Some friends and I have been running webpack-china for a few months. A...

2298
来自专栏我爱编程

conda update

DESCRIPTION usage: conda update [-h] [-y] [--dry-run] [-f] [--file F...

1202
来自专栏Aloys的开发之路

Java代码质量度量工具大阅兵

FindBugs, a program which uses static analysis to look for bugs in Java code. It...

712

扫码关注云+社区