Skip to content

Commit

Permalink
增加 Swagger Starter 的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Nov 28, 2020
1 parent ce180d3 commit c39d559
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lab-24/lab-24-apidoc-swagger-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-24-apidoc-swagger-starter</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 实现对 Swagger 的自动配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab24;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cn.iocoder.springboot.lab24.config;

import org.springframework.context.annotation.Bean;
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;

//@Configuration
// @EnableSwagger2 无需使用该注解
public class SwaggerConfiguration {

@Bean
public Docket createRestApi() {
// 创建 Docket 对象
return new Docket(DocumentationType.SWAGGER_2) // 文档类型,使用 Swagger2
.apiInfo(this.apiInfo()) // 设置 API 信息
// 扫描 Controller 包路径,获得 API 接口
.select()
.apis(RequestHandlerSelectors.basePackage("cn.iocoder.springboot.lab24.controller"))
.paths(PathSelectors.any())
// 构建出 Docket 对象
.build();
}

/**
* 创建 API 信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("测试接口文档示例")
.description("我是一段描述")
.version("1.0.0") // 版本号
.contact(new Contact("芋艿", "http://www.iocoder.cn", "[email protected]")) // 联系人
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cn.iocoder.springboot.lab24.controller;

import cn.iocoder.springboot.lab24.dto.UserAddDTO;
import cn.iocoder.springboot.lab24.dto.UserUpdateDTO;
import cn.iocoder.springboot.lab24.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/users")
@Api(tags = "用户 API 接口")
public class UserController {

@GetMapping("/list")
@ApiOperation(value = "查询用户列表", notes = "目前仅仅是作为测试,所以返回用户全列表")
public List<UserVO> list() {
// 查询列表
List<UserVO> result = new ArrayList<>();
result.add(new UserVO().setId(1).setUsername("yudaoyuanma"));
result.add(new UserVO().setId(2).setUsername("woshiyutou"));
result.add(new UserVO().setId(3).setUsername("chifanshuijiao"));
// 返回列表
return result;
}

@GetMapping("/get")
@ApiOperation("获得指定用户编号的用户")
@ApiImplicitParam(name = "id", value = "用户编号", paramType = "query", dataTypeClass = Integer.class, required = true, example = "1024")
public UserVO get(@RequestParam("id") Integer id) {
// 查询并返回用户
return new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
}

@PostMapping("add")
@ApiOperation("添加用户")
public Integer add(UserAddDTO addDTO) {
// 插入用户记录,返回编号
Integer returnId = UUID.randomUUID().hashCode();
// 返回用户编号
return returnId;
}

@PostMapping("/update")
@ApiOperation("更新指定用户编号的用户")
public Boolean update(UserUpdateDTO updateDTO) {
// 更新用户记录
Boolean success = true;
// 返回更新是否成功
return success;
}

@PostMapping("/delete")
@ApiOperation(value = "删除指定用户编号的用户")
@ApiImplicitParam(name = "id", value = "用户编号", paramType = "query", dataTypeClass = Integer.class, required = true, example = "1024")
public Boolean delete(@RequestParam("id") Integer id) {
// 删除用户记录
Boolean success = false;
// 返回是否更新成功
return success;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.iocoder.springboot.lab24.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用户添加 DTO")
public class UserAddDTO {

@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
private String username;
@ApiModelProperty(value = "密码", required = true, example = "nicai")
private String password;

public String getUsername() {
return username;
}

public UserAddDTO setUsername(String username) {
this.username = username;
return this;
}

public String getPassword() {
return password;
}

public UserAddDTO setPassword(String password) {
this.password = password;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cn.iocoder.springboot.lab24.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用户更新 DTO")
public class UserUpdateDTO {

@ApiModelProperty(value = "用户编号", required = true, example = "1024")
private Integer id;
@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
private String username;
@ApiModelProperty(value = "密码", required = true, example = "nicai")
private String password;

public Integer getId() {
return id;
}

public UserUpdateDTO setId(Integer id) {
this.id = id;
return this;
}

public String getUsername() {
return username;
}

public UserUpdateDTO setUsername(String username) {
this.username = username;
return this;
}

public String getPassword() {
return password;
}

public UserUpdateDTO setPassword(String password) {
this.password = password;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cn.iocoder.springboot.lab24.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("用户 VO")
public class UserVO {

@ApiModelProperty(value = "用户编号", required = true, example = "1024")
private Integer id;
@ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
private String username;

public Integer getId() {
return id;
}

public UserVO setId(Integer id) {
this.id = id;
return this;
}

public String getUsername() {
return username;
}

public UserVO setUsername(String username) {
this.username = username;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 对应 SpringfoxConfigurationProperties 配置类
springfox:
documentation:
swagger-ui:
enabled: true # 是否开启 Swagger UI 功能。默认为 true
1 change: 1 addition & 0 deletions lab-24/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<module>lab-24-apidoc-swagger</module>
<module>lab-24-apidoc-swagger-knife4j</module>
<module>lab-24-apidoc-japidocs</module>
<module>lab-24-apidoc-swagger-starter</module>
</modules>


Expand Down

0 comments on commit c39d559

Please sign in to comment.