springBoot 集成swagger2.9.2
加依赖
<!-- 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的更多相关文章
- SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)
1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- springboot集成swagger2构建RESTful API文档
在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...
- SpringBoot集成Swagger2在线文档
目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...
- springboot 集成swagger2.x 后静态资源报404
package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...
- SpringBoot集成Swagger2并配置多个包路径扫描
1. 简介 随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...
- springboot集成swagger2报Illegal DefaultValue null for parameter type integer
springboot集成swagger2,实体类中有int类型,会报" Illegal DefaultValue null for parameter type integer"的 ...
- SpringBoot集成Swagger2 以及汉化 快速教程
(一) Swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件 (二)为什么使用Swagger 在现在的开发过程中还有很大一部分公司都是以口口相传的方式来进行 ...
- Springboot集成swagger2生成接口文档
[转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html 作者:jstarseven 码字挺辛苦的..... 一 ...
- springboot 集成swagger2
使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...
- [转] spring-boot集成swagger2
经测,spring-boot版本使用1.5.2+时需使用springfox-swagger2版本2.5+(spring-boot 1.2 + springfox-swagger2 2.2 在未扫描ja ...
随机推荐
- Mac中文输入法无选字框了?
按F4键,把其它中的活动监视器打开,选简体中文输入法进程,双击点退出 ,再点强制退出,关了吧.再打字,好用了-
- CC18:二叉树平衡检查
题目 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1. 给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是否平衡 ...
- 集合中的 for-Each循环
数组的加强型的for-Each循环很简单,我们再来看一下集合中的for-Each 循环又是怎么样的.我们都知道集合中的遍历都是通过迭代(iterator)完成的.也许有人说,也可以按照下面的方式来遍 ...
- 简单记录下SpringCloud的微服务架构和一些概念
一.微服务的注册与发现——Eureka 和许多分布式设计一样,分布式的应用一般都会有一个服务中心,用于记录各个机器的信息.微服务架构也一样,我们把一个大的应用解耦成这么多个那么多个服务,那么在想要调用 ...
- PIX 防火墙
---恢复内容开始--- 一 , PIX 防火墙的认识 PIX 是cisco 的硬件防火墙 硬件防火墙的工作速度快,使用方便. PIX 有很多型号,并发连接数是PIX防火墙的重要参数 PIX 25 ...
- java lombok包在maven已经配置,但是注解没用
如果你是用eclipse作为开发环境,配置了maven依赖以后,还需要在eclipse/myeclipse中手动安装lombok. 其实就是加一个jar包,添加2行代码 1. 将 lombok.jar ...
- CentOS 6.4 中yum命令安装php5.2.17
最近给公司部署服务器的时候发现他们提供的服务器是centos6.4系统的,装好系统和相关服务httpd,mysql,php,一跑代码,发现php5.3中的zend加密不能用,安装Zend Guard ...
- 关于Identityserver4和IdentityServer3 授权不兼容的问题
使用IdentityServer3 作为授权服务器,如果没有设置证书,而且client又没有设置AccessTokenType = AccessTokenType.Reference,则获取token ...
- Flat UI theme--扁平化的UI
项目地址:点击打开 支持版本: jQuery Mobile 1.3.2 使用很简单,前提是你的前端是在jquery-mobile的基础上开发的,然后导入相应的css文件.img文件和js文件即可. 案 ...
- JAVA的API部分介绍
个人理解: Object作为最大的父类,里面存在不少方法,可以在API中具体的查找.比如可以帮助查看是否相同的equals方法,不过要想看具体属性是否相同需要得重写,打印.调用对象相当于调用其tost ...