①、导入依赖

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.2.2</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.2.2</version>

</dependency>

②、创建swagger2配置类

@Configuration              //声明配置类
@EnableSwagger2             //开启在线文档
public class SwaggerConfig {
    //1 声明api  文档的属性 构建器
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("swagger2文档生成测试").description("测试使用")
                .termsOfServiceUrl("http://ujiuye.com/").contact("java0708")
                .version("1.0").build();
    }
    //2 配置核心配置信息
    public Docket insertDocket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))
                .paths(PathSelectors.any()).build();
    }
}

③、修改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";

}

④、查看swagger2文档

访问地址:http://localhost:8080/swagger-ui.html

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

swagger2注解介绍

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的 等等,如:

@ApiIgnore:使用注解忽略该API,不会参与文档生成

@ApiOperation:描述该api,如: @ApiOperation(value=”创建用户”, notes=”根据User 对象创建用户”)

请求方法:@RequestMapping(value = “user”, method = RequestMethod.POST)

参数x信息:@ApiImplicitParam(name = “user”, value = “用户详细实体user”, required = true, dataType = “User”)

@Api:修饰整个类,描述Controller的作用

@ApiParam:单个参数描述

@ApiModel:用对象来接收参数

@ApiResponses:HTTP响应整体描述

@ApiProperty:用对象接收参数时,描述对象的一个字段

swagger2文档使用的更多相关文章

  1. SpringBoot之Swagger2文档生成

    SpringBoot之Swagger2文档生成 1.Swagger2介绍 编写和维护接口文档是每个程序员的职责,前面我们已经写好的接口现在需要提供一份文档,这样才能方便调用者使用.考虑到编写接口文档是 ...

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

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

  3. 将Swagger2文档导出为HTML或markdown等格式离线阅读

    网上有很多<使用swagger2构建API文档>的文章,该文档是一个在线文档,需要使用HTTP访问.但是在我们日常使用swagger接口文档的时候,有的时候需要接口文档离线访问,如将文档导 ...

  4. SpringBoot2 整合 Swagger2文档 使用BootstrapUI页面

    SpringBoot2 整合 Swagger2 SpringBoot整合三板斧 第一步.引入pom <dependency> <groupId>com.spring4all&l ...

  5. spring boot☞Swagger2文档构建及单元测试

    首先,回顾并详细说明一下在快速入门中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  6. Spring Boot中使用Swagger2构建API文档

    程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...

  7. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  8. swagger2的接口文档

    以前见过一个swagger2的接口文档,特别好用,好看,对接口中入参描述的很详细:适合用于项目的开发 后来自己做项目的时候,没有找到这个swagger版本 <dependency> < ...

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

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

随机推荐

  1. javaScript 例子

    1.a标签调用js的几种方法 a href="javascript:void(0);" onclick="js_method()" a href="# ...

  2. CSS高度坍塌原因及解决办法

    在文档流中,父元素的高度默认是被子元素撑开的,也就是子元素多高,父元素就多高. 但是当为子元素设置浮动以后,子元素会完全脱离文档流,此时将会导致子元素无法撑起父元素的高度,导致父元素的高度塌陷.由于父 ...

  3. Simple Vedio Intercom System

    I. Deployment  / Architecture Block Diagram II. Resources Used sip proxy server + sip user agent 1.  ...

  4. Python PostgreSQL Psycopg2

    [转] http://daigong.iteye.com/blog/901160 Python如果要操作Postgresql,需要一个API,这就需要Psycopg2 1. 链接PostgreSQL并 ...

  5. python使用etcd

    import sys import etcd client = etcd.Client( host='127.0.0.1', port=2379, allow_reconnect=True) clie ...

  6. no hash tools

    import itertools class Set(list):    def __init__(self, params):        super(Set, self).__init__()  ...

  7. flask之Twitter Bootstrap

    一:Twitter Bootstrap是什么? 1.开源框架:提供用户页面组件. 2.可以创建整洁且具有吸引力的网站,并且网站能兼容所有现代的Web浏览器. 特点: Bootstrap 是客户端框架, ...

  8. a[b]和b[a]区别

    #include <stdio.h> main() { char a[5] = "abcd"; int b = 3; printf("%c\n",a ...

  9. iText导出PDF(图片,水印,页眉,页脚)

    项目需要导出PDF,导出的内容包含图片和文本,而且图片的数量不确定,在网上百度发现大家都在用iText,在官网发现可以把html转换为PDF,但是需要收费,那就只能自己写了. 在开始之前先在网上百度了 ...

  10. git怎样删除未监视的文件untracked files ?

    git怎样删除未监视的文件untracked files 需要添加到.gitignore文件 # 删除 untracked files git clean -f # 连 untracked 的目录也一 ...