加依赖

        <!-- 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. 解决IE6 IE7绝对定位弹层被后面的元素遮住

    解决IE6 IE7绝对定位弹层被后面的元素遮住? 弹层边框一直被后面的元素边框遮住,试了n种方法,只有这个比较好用. <div class="tuijian-table"&g ...

  2. [软件工程基础]Alpha 阶段事后分析

    设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 帮助选修物理实验的学生撰写实验报告,计算实验数据,验证计算结果,并提供一个讨论的平台. 全体成员认 ...

  3. [LOJ 2082] 「JSOI2016」炸弹攻击 2

    [LOJ 2082] 「JSOI2016」炸弹攻击 2 链接 链接 题解 枚举发射源,将发射源当做原点,对敌人和激光塔极角排序. 由于敌人纵坐标均为正,而其它点均为负,因此每两个角度差在 \(\pi\ ...

  4. CSS3 - CheakBox 开关效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Magic Maze dfs + dp

    http://swjtuoj.cn/problem/2387/ 设dp[cur]表示以cur这个节点为起点的时候,能走的最大贡献. 题目保证没环,也就是没回路. 感觉和树dp差不多了. #includ ...

  6. (转)Linux命令之Ethtool用法详解

    Linux命令之Ethtool用法详解 原文:http://www.linuxidc.com/Linux/2012-01/52669.htm Linux/Unix命令之Ethtool描述:Ethtoo ...

  7. Unity3d Attribute 总结(转)

      举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值. 在Class上使用[RequireComponent]属性,就会 ...

  8. 20170405JDBC数据查询

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  9. android studio项目提交Git@OSC

    转载地址:http://www.bubuko.com/infodetail-977061.html 先到git.oscchina.net网站上申请个账号,然后创建一个项目.过程不再说了. 新建工程后, ...

  10. SaaS 系统架构设计经验总结

    2B SaaS系统最近几年都很火.很多创业公司都在尝试创建企业级别的应用 cRM, HR,销售, Desk SaaS系统.很多SaaS创业公司也拿了大额风投.毕竟SaaS相对传统软件的优势非常明显. ...