加依赖

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

springBoot的application.java的同级目录下新建SwaggerConfiguration类

package com.example.demo;

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; @Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) //过滤的接口
.paths(PathSelectors.any())
.build();
} private ApiInfo getApiInfo() {
// 定义联系人信息
Contact contact = new Contact("name","https://baidu.com", "test@test.com");
return new ApiInfoBuilder()
.title("标题")
.description("描述")
.version("版本")
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.contact(contact)
.build();
}
}

controller类:

@RestController
@Api(value = "/user",description = "这个是用户信息 ",tags = "用户信息")
@RequestMapping("/user")
public class UserContrller { @ApiOperation(value="获取用户列表", notes="")
@GetMapping("")
public List<User> getUserList() {
return null;
} @ApiOperation(value="创建用户", notes="根据User对象创建用户")
@PostMapping("")
public String postUser(@RequestBody User user) {
return "success";
}
}

浏览器输入 http://localhost:8080/swagger-ui.html  访问

这样就完成了, 接下来就是去搜Swagger的常用注解怎么使用就ok了。 附上链接: https://github.com/swagger-api/swagger-core/wiki/annotations

——————————————————————————————————————

以下是我遇到的部分问题

@ApiImplicitParam 注解: 如果参数是实体类并且实体类中被@ApiModel和@ApiModelProperty注解修饰过,  @ApiImplicitParam注解就不要加了。

如果,没有实体类没有被@ApiModel和@ApiModelProperty注解修饰过, @ApiImplicitParam可加可不加,  另外在实体类参数之前加上@RequestBody 和不加@RequestBody,swagger的文档参数显示是不一样的

比如不加@RequestBody注解的代码:

@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@PostMapping("")
public String postUser(User user) {
users.put(user.getId(), user);
return "success";
}

User实体:

package com.example.demo.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @ApiModel(value = "用户信息", description = "这个用户信息只用于测试")
@Data
public class User { @ApiModelProperty("id")
private Long id; @ApiModelProperty(value="姓名",required=true)
private String name; @ApiModelProperty("年龄")
private Integer age;
}

swagger显示的参数是这样的

加了@RequestBody

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
@PostMapping("")
public String postUser(@RequestBody User user) {
users.put(user.getId(), user);
return "success";
}

加了@RequestBody的swagger:

springBoot 集成swagger2.9.2的更多相关文章

  1. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  2. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  3. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  4. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  5. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  6. springboot集成swagger2报Illegal DefaultValue null for parameter type integer

    springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...

  7. SpringBoot集成Swagger2 以及汉化 快速教程

    (一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...

  8. Springboot集成swagger2生成接口文档

    [转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html    作者:jstarseven    码字挺辛苦的.....   一 ...

  9. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  10. [转] spring-boot集成swagger2

    经测,spring-boot版本使用1.5.2+时需使用springfox-swagger2版本2.5+(spring-boot 1.2 + springfox-swagger2 2.2 在未扫描ja ...

随机推荐

  1. JavaScript进阶 - 第7章 JavaScript内置对象

    第7章 JavaScript内置对象 7-1 什么是对象 JavaScript 中的所有事物都是对象,如:字符串.数值.数组.函数等,每个对象带有属性和方法. 对象的属性:反映该对象某些特定的性质的, ...

  2. Node.js 第三方包的安装、升级、卸载,以及包依赖管理

    本地安装: npm install package-name 全局全装: npm install -g  package-name 举个栗子 全局安装 React项目的脚手架 npm install ...

  3. 从wireshake分析http和https的通信过程

    参考文章: Wireshark基本介绍和学习TCP三次握手 [技术流]Wireshark对HTTPS数据的解密 Wireshark/HTTPS Journey to HTTP/2 以TCP/IP协议为 ...

  4. Netty(5)@Sharable

    问题:我写了MyDecoder which extends ByteToMessageDecoder,单线程没问题,但是多线程时,报'the handler should be sharable'.查 ...

  5. JS——变量声明、变量类型、命名规范

    变量声明: JavaScript是一种弱类型语言,它的变量类型由它的值来决定,var是变量声明. 变量类型: 基本类型:number.string.boolean(布尔类型:var a=true/fa ...

  6. spoolight on oracle 配置

    spoolight seting 1ORACLE_HOME=D:\oracle\product\11.2.0\client_1set SQLPATH=D:\oracle\product\11.2.0\ ...

  7. 关于C#操作Excel,复制Sheet的记录

    1.先用了NPOI,去做,HSSFWorkbook 里面有一个Copy方法,但这个只支持office2003. 对应的XSSFWorkbook没有些方法. 而且这个这个方法对devexpress导出的 ...

  8. C#远程连接sqlserver时,尝试读取或写入受保护的内存

    管理员身份运行 cmd ->  输入 netsh winsock reset

  9. vi命令使用

    在vi下如何显示行号? 按Esc切换到命令行模式,输入: :set nu 如果您想每次进入vi都标出行号,编辑~/.vimrc文件.也就是在用户的主目录下,编辑存档.vimrc文件.里边写一行: se ...

  10. 求一个极大数的欧拉函数 phi(i)

    思路: 因为当n>=1e10的时候,线性筛就不好使啦.所以要用一个公式 φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn) 证明详见:<公式 ...