From c39d559f528182b12ef16521d9ea74c19b922943 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sat, 28 Nov 2020 10:47:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Swagger=20Starter=20?= =?UTF-8?q?=E7=9A=84=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab-24/lab-24-apidoc-swagger-starter/pom.xml | 30 ++++++++ .../iocoder/springboot/lab24/Application.java | 13 ++++ .../lab24/config/SwaggerConfiguration.java | 41 +++++++++++ .../lab24/controller/UserController.java | 68 +++++++++++++++++++ .../springboot/lab24/dto/UserAddDTO.java | 32 +++++++++ .../springboot/lab24/dto/UserUpdateDTO.java | 43 ++++++++++++ .../iocoder/springboot/lab24/vo/UserVO.java | 32 +++++++++ .../src/main/resources/application.yaml | 5 ++ lab-24/pom.xml | 1 + 9 files changed, 265 insertions(+) create mode 100644 lab-24/lab-24-apidoc-swagger-starter/pom.xml create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/Application.java create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/config/SwaggerConfiguration.java create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/controller/UserController.java create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserAddDTO.java create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserUpdateDTO.java create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/vo/UserVO.java create mode 100644 lab-24/lab-24-apidoc-swagger-starter/src/main/resources/application.yaml diff --git a/lab-24/lab-24-apidoc-swagger-starter/pom.xml b/lab-24/lab-24-apidoc-swagger-starter/pom.xml new file mode 100644 index 000000000..91dbbee69 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/pom.xml @@ -0,0 +1,30 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.11.RELEASE + + + 4.0.0 + + lab-24-apidoc-swagger-starter + + + + + org.springframework.boot + spring-boot-starter-web + + + + + io.springfox + springfox-boot-starter + 3.0.0 + + + + diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/Application.java b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/Application.java new file mode 100644 index 000000000..cc795da46 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/Application.java @@ -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); + } + +} diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/config/SwaggerConfiguration.java b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/config/SwaggerConfiguration.java new file mode 100644 index 000000000..1cc1fddd4 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/config/SwaggerConfiguration.java @@ -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", "zhijiantianya@gmail.com")) // 联系人 + .build(); + } + +} diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/controller/UserController.java b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/controller/UserController.java new file mode 100644 index 000000000..6ed0a76f4 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/controller/UserController.java @@ -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 list() { + // 查询列表 + List 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; + } + +} diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserAddDTO.java b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserAddDTO.java new file mode 100644 index 000000000..af181e690 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserAddDTO.java @@ -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; + } + +} diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserUpdateDTO.java b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserUpdateDTO.java new file mode 100644 index 000000000..3c92aeb8b --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/dto/UserUpdateDTO.java @@ -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; + } + +} diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/vo/UserVO.java b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/vo/UserVO.java new file mode 100644 index 000000000..4917916d9 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/java/cn/iocoder/springboot/lab24/vo/UserVO.java @@ -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; + } + +} diff --git a/lab-24/lab-24-apidoc-swagger-starter/src/main/resources/application.yaml b/lab-24/lab-24-apidoc-swagger-starter/src/main/resources/application.yaml new file mode 100644 index 000000000..c4573845e --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger-starter/src/main/resources/application.yaml @@ -0,0 +1,5 @@ +# 对应 SpringfoxConfigurationProperties 配置类 +springfox: + documentation: + swagger-ui: + enabled: true # 是否开启 Swagger UI 功能。默认为 true diff --git a/lab-24/pom.xml b/lab-24/pom.xml index 1c5c69a04..7d0bf03c5 100644 --- a/lab-24/pom.xml +++ b/lab-24/pom.xml @@ -15,6 +15,7 @@ lab-24-apidoc-swagger lab-24-apidoc-swagger-knife4j lab-24-apidoc-japidocs + lab-24-apidoc-swagger-starter