简介

  • springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API;

  • swagger主要用于展示springfox生成的API文档;

依赖

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

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>
  • pringfox-swagger2依赖OSA规范文档,也就是一个描述API的json文件,而这个组件的功能就是帮助我们自动生成这个json文件;

  • 我们会用到的另外一个组件springfox-swagger-ui就是将这个json文件解析出来,用一种更友好的方式呈现出来;

使用方法1,只需要一步@EnableSwagger2

@EnableSwagger2
@SpringBootApplication
public class BlogApplication {
    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
}

结果是默认样式:

使用方法2,自定义Java代码

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;

/**
 * Author:Mr.X
 * Date:2017/10/7 16:11
 * Description:
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {

    /**
     * 创建Docket
     * @return
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("Blog Api")
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.mrx.browser.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Bolg 博客系统API文档")
                .contact(new Contact("Mr.X", "http://www.mrx.com", "jxjy.ing@foxmail.com"))
                .version("1.0")
                .description("API 描述")
                .build();
    }
}

结果是自定义样式:

博客参考链接
springboot从小白到大神-008程序猿的痛:写文档

springfox-swagger2的更多相关文章

  1. MP实战系列(十)之SpringMVC集成SpringFox+Swagger2

    该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...

  2. 使用springfox+swagger2书写API文档(十八)

    使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...

  3. SpringBoot中使用springfox+swagger2书写API文档

    随着前后端的分离,借口文档变的尤其重要,springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfox生成的API文 ...

  4. SpringFox Swagger2注解基本用法

    一切参数说明,参考官方API文档:http://docs.swagger.io/swagger-core/current/apidocs/index.html?io/swagger/annotatio ...

  5. springfox+swagger2生成API文档

    1.建立一个spring mvc工程: 2.添加POM依赖: <properties> <springfoxversion>2.6.1</springfoxversion ...

  6. SpringFox swagger2 and SpringFox swagger2 UI 接口文档生成与查看

    依赖: <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency ...

  7. Springfox与swagger的整合使用

    一.前言 让我们先理一下springfox与swagger的关系. swagger是一个流行的API开发框架,这个框架以“开放API声明”(OpenAPI Specification,OAS)为基础, ...

  8. Springfox与swagger的整合使用(十七)

    一.前言 让我们先理一下springfox与swagger的关系. swagger是一个流行的API开发框架,这个框架以“开放API声明”(OpenAPI Specification,OAS)为基础, ...

  9. Spring Boot 2 整合Swagger简单入门

    Swagger是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. 1.pom.xml添加配置 可以到http://mvnrepository.com上搜索springfox,便可以看到Sp ...

  10. JavaWeb界面在线配置代码生成器

    关于直接main方法运行生成代码可参考我的这篇文章:MP实战系列(六)之代码生成器讲解 在线配置主要参考jeesite和jeecg,gun等开源项目,但是与它们相比又有很多不同? 与jeesite相比 ...

随机推荐

  1. 一个时间上的比较 if else

    if (w<b.w) ; if (w>b.w) ; if (w<b.w) ; else if (w>b.w) ; 对于任何情况,执行的次数都是一样.只是对于汇编的代码,第二个方 ...

  2. (逆序对 分治法)P1908 逆序对 洛谷

    题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定 ...

  3. Linux_问题

    1.远程连接项目服务器(miit_shi),遇到的问题 问题:Connecting to IP地址:22... Connection established. To escape to local s ...

  4. flask SQLALchemy外键及约束

    from flask import Flask,session from flask_sqlalchemy import SQLAlchemy import config app = Flask(__ ...

  5. python 发送邮件模板

    ##发送普通txt文件(与发送html邮件不同的是邮件内容设置里的type设置为text,下面代码为发送普通邮件的另一种方法) import smtplibimport stringfrom emai ...

  6. WEB网站类型系统中使用的OFFICE控件

    WEB下使用的OFFICE控件介绍,另提供一个原创破解首先来个名词解释,Office网络文档控件,就是在网页中编辑office文档的控件(前提是browser已经安装OFFICE).最近一个项目需要用 ...

  7. 8种提升ASP.NET Web API性能的方法

    英文原文:8 ways to improve ASP.NET Web API performance ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没 ...

  8. Ubuntu Server 16.04 安装MySQL并设置远程访问

    Ubuntu Server 16.04 安装MySQL 1. 使用root账号 sudo apt-get install mysql-serversudo apt-get install mysql- ...

  9. 【leetcode-82,83,26,80】 删除排序链表/数组中的重复元素

    83. 删除排序链表中的重复元素 (1 pass) 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

  10. LeetCode(193. Valid Phone Numbers)(sed用法)

    193. Valid Phone Numbers Given a text file file.txt that contains list of phone numbers (one per lin ...