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 ...
随机推荐
- 18-----BBS论坛
BBS论坛(十八) 18.首页轮播图实现 (1)front/css/front_base.css .main-container{ width: 990px; margin: 0 auto; over ...
- python3.7安装Scrapy
环境:windows 7. 安装过程中遇到的问题 1.error: Unable to find vcvarsall.bat 2.1083: Cannot open include file: 'ba ...
- cesm1_2_2在南信大大型机上的移植以及运行简单case的步骤
真实验证有效:点击链接 查看具体移植过程.
- HTML_DOM学习
HTML DOM 树 通过ID/类名/标号可以定位HTML元素,然后可用JS改变这些元素的样式内容,并对DOM事件作出反应 对HTML事件的响应: onmousedown 和onmouseup/onc ...
- fireFox在中国的https网站的时候,老会出 ssl_error_unsupported_version 这个错误。
fireFox在中国的https网站的时候,老会出 ssl_error_unsupported_version 这个错误. 出现在 这个的解决办法就是 在地址栏里输入 about:config 查找 ...
- Murano Setup Steps
1. Select a Linux Distribution Only Ubuntu 14.04 (Trusty), Fedora 21 (or Fedora 22) and CentOS/RHEL ...
- 深入学习webpack(四)
更多内容可以看此文档.
- ThreadLocal 开启事务
1.ThreadLocal该类提供了线程局部变量 2.分析原理: ThreadLocal内部有一个Map.Map的key是当前线程对象,value是一个Object对象. 模拟该类: public c ...
- jquery拖拽排序,针对后台列表table进行拖拽排序(超实用!)
现在很多后台列表为了方便均使用拖拽排序的功能,对列表进行随意的排序. 话不多说 ,我在网上找了一些demo,经过对比,现在把方便实用的一个demo列出来,基于jqueryUI.js 先上html代码, ...
- 5.vs的各个组成部分
启动vs (1).双击vs的打开图标 (2).输入devenv 编写项目时,命名的时候,尽量不要使用中文. 我们暂时将.cs文件理解为是一个类文件. 在 视图菜单 下可以打开 解决方案资源管理器 ...