Spring boot 集成 Swagger
添加依赖包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
开启注解
@EnableSwagger2
示例如下:
1. 新建 Maven 项目 swagger
2. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.java</groupId>
<artifactId>swagger</artifactId>
<version>1.0.0-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent> <!-- 配置版本常量 -->
<properties>
<jdk.version>1.8</jdk.version>
</properties> <dependencies> <!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency> </dependencies> <build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. SwaggerStarter.java
package com.java.swagger; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SwaggerStarter { public static void main(String[] args) {
SpringApplication.run(SwaggerStarter.class, args);
} }
4. SwaggerConfig.java
package com.java.swagger.config; 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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2);
ApiSelectorBuilder builder = docket.apiInfo(apiInfo()).select();
builder.apis(RequestHandlerSelectors.basePackage("com.java.swagger.controller"));
builder.paths(PathSelectors.any());
return builder.build();
} private ApiInfo apiInfo() {
ApiInfoBuilder builder = new ApiInfoBuilder();
builder.title("我的项目Swagger");
builder.description("简单易用的API文档");
builder.version("V1.0");
return builder.build();
} }
5. User.java
package com.java.swagger.pojo;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
public class User {
@ApiModelProperty("用户名")
private String name;
@ApiModelProperty("年龄")
private int age;
@ApiModelProperty(value = "生日", example = "2018-12-31 10:00:00")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date birthDay;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
}
6. UserController.java
package com.java.swagger.controller; import java.util.Calendar;
import java.util.Date; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.java.swagger.pojo.User; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; @RestController
@RequestMapping("/user")
public class UserController { @GetMapping("/getUesr/{name}")
@ApiOperation(value = "查询用户信息", notes = "各种解释说明信息")
public User getUser(@ApiParam("用户名") @PathVariable String name) {
User user = new User();
user.setName(name);
user.setAge(18);
user.setBirthDay(new Date());
return user;
} @PostMapping("/addUser")
@ApiOperation(value = "新增用户", notes = "各种解释说明信息")
public User addUser(@ApiParam("用户信息") @RequestBody User user) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(user.getBirthDay());
calendar.add(Calendar.DAY_OF_MONTH, 1);
user.setBirthDay(calendar.getTime()); user.setAge(user.getAge() + 1);
return user;
} }
7. 启动项目,浏览器输入
http://localhost:8080/swagger-ui.html
效果如下图:

.
Spring boot 集成 Swagger的更多相关文章
- Spring Boot 集成 Swagger,生成接口文档就这么简单!
之前的文章介绍了<推荐一款接口 API 设计神器!>,今天栈长给大家介绍下如何与优秀的 Spring Boot 框架进行集成,简直不能太简单. 你所需具备的基础 告诉你,Spring Bo ...
- Spring Boot 集成Swagger
Spring Boot 集成Swagger - 小单的博客专栏 - CSDN博客https://blog.csdn.net/catoop/article/details/50668896 Spring ...
- spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件
本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...
- 【Swagger】可能是目前最好的 Spring Boot 集成 swagger 的方案
[Swagger]可能是目前最好的Spring Boot集成 swagger 的方案  Swagger 是一个规范和完整的框架,用于生成.描述. ...
- spring boot集成swagger文档
pom <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifa ...
随机推荐
- 2.1 GO 变量定义
GO有四种数据类型,数字.布尔.字符.派生类型:这里使用前三种简单类型来说明变量的定义与使用 package main import "fmt" var ( aa = 1 bb = ...
- webpack01
- 2018.6.1学习CSS5里顺丰盒子小问题
在制作下面这样的小盒子时 编写的代码 首先要对li元素设置浮动. 在设置这个li元素下面的a元素时, 因为没有转行内块,inline-block,结果显示出来的页面,鼠标在经过这个选择时(:hover ...
- $('#').formValidation校验网址
$('#addCarouselInfoForm').formValidation({ message: '格式不正确', //不忽略隐藏域验证 excluded: [], icon: { valid: ...
- git 基础教程
git 提交 全部文件 git add . git add xx命令可以将xx文件添加到暂存区,如果有很多改动可以通过 git add -A .来一次添加所有改变的文件.注意 -A 选项后面还有一个 ...
- 案例49-crm练习获取客户列表带有分页和筛选功能
1 案例分析 2 书写步骤 1.封装PageBean 2.书写Action 3.书写Service 4.书写Dao 注意清空之前设置的聚合函数 dc.setProjection(null); 5 ...
- PHP 7 的几个新特性
1. ?? 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用.用法: $a = $_GET['a'] ?? 1; 它相当于: <?php $a = isset($_GET[ ...
- GitKraken使用教程-基础部分(6)
4) 放弃本次文件的改动 有些情况下,由于更改代码造成了编译无法通过等错误时,想要放弃这次对文件的修改,将文件还原成上一次提交后的状态,一种简单的恢复文件的方法就是,在Unstaged Files 列 ...
- GitKraken使用教程-基础部分(3)
5. 克隆服务器上的项目 首先,返回主界面,点击File => Clone Repo,选择 Clone with URL,如下图: 图 5‑1 SSH方式克隆仓库界面 1) SSH 方式连接仓库 ...
- net 总数据中取随机几条数据
List<string> lstSample = new List<string>(); Random rand = new Random(); List<int> ...