SpringBoot之Swagger2文档生成

1、Swagger2介绍

编写和维护接口文档是每个程序员的职责,前面我们已经写好的接口现在需要提供一份文档,这样才能方便调用者使用。考虑到编写接口文档是一个非常枯燥的工作,我们采用Swagger2这套自动化文档工具来生成文档,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。

2、SpringBoot开启Swagger2支持

第一步:在pom.xml中加入Swagger2的依赖

<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>

第二步:创建Swagger2配置类

package com.offcn.config;

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;

@Configuration

@EnableSwagger2

public class SwaggerConfig {

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("Spring Boot中使用Swagger2构建RESTful APIs")

.description("优就业")

.termsOfServiceUrl("http://www.ujiuye.com/")

.contact("Sunny")

.version("1.0")

.build();

}

}

3、修改Controller增加文档注释

通过@ApiOperation注解来给API增加说明 通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明

/**

* 更新指定id用户信息

* @param id

* @param user

* @return

*/

@PutMapping("/{id}")

@ApiOperation(value="更新指定id用户信息", notes="根据id更新用户信息")

@ApiImplicitParams({

@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),

@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")

})

public String updateUser(@PathVariable("id") Long id,User user) {

user.setId(id);

userRepository.saveAndFlush(user);

return "success";

}

/***

* 删除指定id用户

* @param id

* @return

*/

@DeleteMapping("/{id}")

@ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")

@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long")

public String deleteUser(@PathVariable("id") Long id) {

userRepository.deleteById(id);

return "success";

}

4、查看Swagger2文档

重启应用访问地址:http://localhost:8080/swagger-ui.html

点开每个接口,可以查看接口详情

SpringBoot之Swagger2文档生成的更多相关文章

  1. swagger2文档使用

    ①.导入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  2. SpringBoot(六) SpringBoot整合Swagger2(自动化生成接口文档)

    一:在上篇文章pom增加依赖: <dependency> <groupId>io.springfox</groupId> <artifactId>spr ...

  3. springboot+swagger接口文档企业实践(上)

    目录 1.引言 2.swagger简介 2.1 swagger 介绍 2.2 springfox.swagger与springboot 3. 使用springboot+swagger构建接口文档 3. ...

  4. 【转载】Java Restful API 文档生成工具 smart-doc

    谁说生成api文档就必须要定义注解? 谁说生成接口请求和返回示例必须要在线? 用代码去探路,不断尝试更多文档交付的可能性. 如果代码有生命,为什么不换种方式和它对话! 一.背景 没有背景.就自己做自己 ...

  5. springboot+swagger接口文档企业实践(下)

    目录 1.引言 2. swagger接口过滤 2.1 按包过滤(package) 2.2 按类注解过滤 2.3 按方法注解过滤 2.4 按分组过滤 2.4.1 定义注解ApiVersion 2.4.2 ...

  6. Spring Boot项目使用Swagger2文档教程

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 前言 Sprin ...

  7. 【C#附源码】数据库文档生成工具支持(Excel+Html)

    [2015] 很多时候,我们在生成数据库文档时,使用某些工具,可效果总不理想,不是内容不详细,就是表现效果一般般.很多还是word.html的.看着真是别扭.本人习惯用Excel,所以闲暇时,就简单的 ...

  8. 微软开源全新的文档生成工具DocFX

    微软放弃Sandcastle有些年头了,微软最近开源了全新的文档生成工具DocFX,目前支持C#和VB,类似JSDoc或Sphinx,可以从源代码中提取注释生成文档之外,而且还有语法支持你加入其他的文 ...

  9. DBImport v3.44 中文版发布:数据库数据互导及文档生成工具(IT人员必备)

    前言: 距离上一个版本V3.3版本的文章发布,已经是1年10个月前的事了. 其实版本一直在更新,但也没什么大的功能更新,总体比较稳定,所以也不怎么写文介绍了. 至于工作上的事,之前有半年时间跑去学英语 ...

随机推荐

  1. 使用xkbeancomparator对比javabean,生成操作记录

    xkbeancomparator是一个 java bean 对比修改并输出差异的工具.github地址 适用场景:用户编辑提交时,需要记录修改内容,修改前后的值对比,生成操作记录:可以选择记录的字段和 ...

  2. 【spring】全局异常 globalexception 处理

    全局异常 globalexception 处理   一般在做api开发时我们希望将所有业务层抛到controller异常都集中处理一下.比如对异常差异化报警.转发不同页面.封装不同http状态码.集中 ...

  3. Linux中用postfix搭建邮件服务器实战详解

    Linux中用postfix搭建邮件服务器实战详解 postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.Postfix试图更快.更容易管理.更安全,同时 ...

  4. HTTPS与HTTP区别

    HTTP + 加密 + 认证 + 完整性保护 = HTTPS http的全称是Hypertext Transfer Protocol Vertion (超文本传输协议) HTTPS: HTTPS(Se ...

  5. afnetwork使用

    Usage HTTP Request Operation Manager AFHTTPRequestOperationManager encapsulates the common patterns ...

  6. vue-v-for

    1.v-for遍历数组和对象 <ul> <li v-for="item in array">{{item}}</li><br> &l ...

  7. PowerShell美化

    转载自Powershell 美化 --oh-my-posh,作者Zvonimir. PowerShell默认的主题太丑了,用过OhMyZsh之后是无法忍受这种丑陋的,幸好PowerShell有对应的O ...

  8. java多线程执行时主线程的等待

    1.通过thread.join()方式,注意:如果有多个子线程,需要将全部的线程先start,然后再join.代码示例如下: public class Main {     public static ...

  9. tornado 之 RequestHandler(请求)

    RequestHandler from tornado.web import ReuqestHandler 一.利用HTTP协议想服务器传递参数 提取url的特定部分 http://127.0.0.1 ...

  10. input子系统四 input事件处理【转】

    转自:https://blog.csdn.net/qwaszx523/article/details/54139897 转自http://blog.csdn.net/coldsnow33/articl ...