springboot成神之——swagger文档自动生成工具
本文讲解如何在spring-boot中使用swagger文档自动生成工具
目录结构

说明
swagger的使用本身是非常简单的,因为所有的一切swagger都帮你做好了,你所要做的就是专注写你的api信息
swagger自动生成html文档并且打包到jar包中
本文不仅用了swagger,JSR 303注释信息也支持使用
依赖
// springfox-bean-validators 用来支持 JSR 303
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.9.2</version>
</dependency>
SwaggerConfig
package com.springlearn.learn.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig extends WebMvcConfigurationSupport {
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("Spring Boot REST API")
.description("\"REST API 测试\"")
.version("1.0.0")
.license("无")
.licenseUrl("https://www.baidu.com")
.contact(new Contact("yejiawei", "https://www.cnblogs.com/ye-hcj/", "2640199637@qq.com"))
.build();
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.springlearn.learn.controller"))
.paths(regex("/.*"))
.build()
.apiInfo(metaData());
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE").allowedOrigins("*")
.allowedHeaders("*");
}
}
开启api界面
在你做好了上述的步骤以后,运行项目
直接访问 yourapiurl/v2/api-docs 查看json数据
直接访问 yourapiurl/swagger-ui.html 查看文档界面
JSR 303注释信息
@NotNull(message = "...")
@NotBlank(message = "...")
@Pattern(regexp = "[a-z-A-Z]*", message = "...")
@Past(message = "过去的日期...")
@Min(value = 1, message = "必须大于或等于1")
@Max(value = 10, message = "必须小于或等于10")
@Email(message = "Email Address")
Swagger核心注释
@ApiModel 返回json对象描述
@ApiModelProperty 返回json对象属性描述
@ApiResponses 设置状态码含义
@Api 控制器描述
@ApiOperation api描述
@ApiParam api参数描述
User
package com.springlearn.learn.model;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "用户基本信息")
public class User {
// @NotBlank
// @Size(min = 1, max = 10)
@ApiModelProperty(notes = "用户姓名", example = "叶家伟", required = true, position = 0)
String name;
// @NotBlank
@Pattern(regexp ="[m|f]")
@ApiModelProperty(notes = "用户性别", example = "m", required = true, position = 1)
Character sex;
// @NotNull
// @Min(0)
// @Max(100)
@ApiModelProperty(notes = "用户年龄", example = "19", position = 3)
Integer age;
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public Character getsex() {
return sex;
}
public void setsex(Character sex) {
this.sex = sex;
}
public Integer getage() {
return age;
}
public void setage(Integer age) {
this.age = age;
}
}
TestController
package com.springlearn.learn.controller;
import javax.servlet.http.HttpServletRequest;
import com.springlearn.learn.model.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
@RestController
@Api(value="测试控制器", description="测试控制器描述")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successfully retrieved list"),
@ApiResponse(code = 401, message = "You are not authorized to view the resource"),
@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found")
})
public class TestController {
@ResponseBody
@RequestMapping(value = "/test1/{id}", method = RequestMethod.GET, produces = "application/json")
@ApiOperation(value = "测试api", response = User.class)
public User Test1(HttpServletRequest request, @ApiParam("Id of the person to be obtained. Cannot be empty.") @PathVariable int id){
User user = new User();
user.setname("叶家伟");
user.setage(18);
user.setsex('m');
return user;
}
}
springboot成神之——swagger文档自动生成工具的更多相关文章
- Api文档自动生成工具
java开发,根据代码自动生成api接口文档工具,支持RESTful风格,今天我们来学一下api-doc的生成 作者:互联网编程. 欢迎投稿,一起交流技术 https://www.jianshu.co ...
- Word 2010文档自动生成目录和某页插入页码
一.Word 2010文档自动生成目录 关于Word文档自动生成目录一直是我身边同学们最为难的地方,尤其是毕业论文,经常因为目录问题,被要求修改,而且每次修改完正文后,目录的内容和页码可能都会发生变化 ...
- VS文档自动生成
VS2008文档自动生成 (发现,Sandcastle主要是用于C#项目.里面的注释都是XML格式的.不太适合VC的.最终还是得用Doxygen) 一.Sandcastle简介: Sandcastle ...
- django接口文档自动生成
django-rest_framework接口文档自动生成 只针对用到序列化和返序列化 一般还是用第三方yipi 一.安装依赖 pip3 install coreapi 二.设置 setting.py ...
- 基于 React 开发了一个 Markdown 文档站点生成工具
Create React Doc 是一个使用 React 的 markdown 文档站点生成工具.就像 create-react-app 一样,开发者可以使用 Create React Doc 来开发 ...
- 优于 swagger 的 java markdown 文档自动生成框架-01-入门使用
设计初衷 节约时间 Java 文档一直是一个大问题. 很多项目不写文档,即使写文档,对于开发人员来说也是非常痛苦的. 不写文档的缺点自不用多少,手动写文档的缺点也显而易见: 非常浪费时间,而且会出错. ...
- java 文档自动生成的神器 idoc
写文档 作为一名开发者,每个人都要写代码. 工作中,几乎每一位开发者都要写文档. 因为工作是人和人的协作,产品要写需求文档,开发要写详细设计文档,接口文档. 可是,作为一个懒人,平时最讨厌的一件事情就 ...
- 如何让接口文档自动生成,SpringBoot中Swagger的使用
目录 一.在SpringBoot项目中配置Swagger2 1.pom.xml中对Swagger2的依赖 2.编写配置类启用Swagger 3.配置实体类的文档 4.配置接口的文档 5.访问文档 二. ...
- swagger:API在线文档自动生成框架
传统的API从开发测试开始我们经常借用类似Postman.fiddle等等去做接口测试等等工具:Swagger 为API的在线测试.在线文档提供了一个新的简便的解决方案: NET 使用Swagger ...
随机推荐
- OpenCV几种边缘检测的简例
简单记录一下OpenCV的几种边缘检测函数的用法. 边缘检测算法 以Sobel边缘检测算法为例. Sobel卷积核模板为: 偏导公式为: Gx(i,j)=[f(i+1,j−1)+2f(i+1,j)+f ...
- flask 项目 部署服务器,package安装问题(无外网链接)
1.安装所需的环境/包 1) 在一台开发机器(有网络,编译成功)安装package: pipreqs 语法: pipreqs <项目路径> 将项目所使用的所有包目录将会导出至目录:requ ...
- Codeforces Round #394 (Div. 2) B. Dasha and friends
B. Dasha and friends time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...
- windows server 2016 docker 之创建使用虚拟交换机
windows server 2016 Create a virtual switch for Hyper-V virtual machines 操作步骤: 服务器只有一块网卡连接了网络 尝试1: h ...
- Ganymed实现基本的自动化部署API
Ganymed SSH-2 for Java是一个纯Java实现的SHH2库,官网为http://www.ganymed.ethz.ch/ssh2/,最新的更新时间为2006年10月,在用之前,请仔细 ...
- RAD Studio Mobile Roadmap updated,XE5 will released on next month, Andriod will be supported.
RAD Studio Mobile Roadmap updated Embarcadero updated his RAD Studio Mobile Roadmap. This concern ...
- [转载] FFMPEG之AVRational TimeBase成员理解
FFMPEG的很多结构中有AVRational time_base;这样的一个成员,它是AVRational结构的 typedef struct AVRational{ int num; /// ...
- 51nod 1085 背包问题
在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. 收起 输入 第1行,2个 ...
- 51nod 1011 最大公约数GCD
输入2个正整数A,B,求A与B的最大公约数. 收起 输入 2个数A,B,中间用空格隔开.(1<= A,B <= 10^9) 输出 输出A与B的最大公约数. 输入样例 30 105 输出 ...
- Vue.js 中的动态路由
静态路由是不可以传递参数的.需要传递参数得用到动态路由 那么如何将参数作为路由呢? //在参数名前面加上 : ,然后将参数写在路由的 path 内 routes: [ //将页面组件与path指令的路 ...