From c577bad18a8a00d3e3fc548b89785c004162b0ec Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Tue, 19 Nov 2019 21:48:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20spring=20validation=20?= =?UTF-8?q?=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/pom.xml | 38 +++++++++++ .../springboot/lab24/apidoc/Application.java | 13 ++++ .../apidoc/config/SwaggerConfiguration.java | 43 ++++++++++++ .../apidoc/controller/TestController.java | 67 ++++++++++++++++++ .../apidoc/controller/UserController.java | 68 +++++++++++++++++++ .../lab24/apidoc/dto/UserAddDTO.java | 32 +++++++++ .../lab24/apidoc/dto/UserUpdateDTO.java | 43 ++++++++++++ .../springboot/lab24/apidoc/vo/UserVO.java | 32 +++++++++ lab-24/pom.xml | 19 ++++++ pom.xml | 1 + 10 files changed, 356 insertions(+) create mode 100644 lab-24/lab-24-apidoc-swagger/pom.xml create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/Application.java create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/config/SwaggerConfiguration.java create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/TestController.java create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/UserController.java create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserAddDTO.java create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserUpdateDTO.java create mode 100644 lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/vo/UserVO.java create mode 100644 lab-24/pom.xml diff --git a/lab-24/lab-24-apidoc-swagger/pom.xml b/lab-24/lab-24-apidoc-swagger/pom.xml new file mode 100644 index 00000000..031a38bf --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/pom.xml @@ -0,0 +1,38 @@ + + + + org.springframework.boot + spring-boot-starter-parent + 2.1.3.RELEASE + + + 4.0.0 + + lab-24-apidoc-swagger + + + + + org.springframework.boot + spring-boot-starter-web + + + + + io.springfox + springfox-swagger2 + 2.9.2 + + + + + io.springfox + springfox-swagger-ui + 2.9.2 + + + + + diff --git a/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/Application.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/Application.java new file mode 100644 index 00000000..15c8ed5f --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/Application.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab24.apidoc; + +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/src/main/java/cn/iocoder/springboot/lab24/apidoc/config/SwaggerConfiguration.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/config/SwaggerConfiguration.java new file mode 100644 index 00000000..1f2c70e3 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/config/SwaggerConfiguration.java @@ -0,0 +1,43 @@ +package cn.iocoder.springboot.lab24.apidoc.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.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +@Configuration +@EnableSwagger2 // 标记项目启用 Swagger API 接口文档 +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.apidoc.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/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/TestController.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/TestController.java new file mode 100644 index 00000000..6a66838d --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/TestController.java @@ -0,0 +1,67 @@ +package cn.iocoder.springboot.lab24.apidoc.controller; + +import cn.iocoder.springboot.lab24.apidoc.dto.UserAddDTO; +import cn.iocoder.springboot.lab24.apidoc.dto.UserUpdateDTO; +import cn.iocoder.springboot.lab24.apidoc.vo.UserVO; +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("/tests") +//@Api(tags = "用户 API 接口") +public class TestController { + + @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(paramType = "query", dataType = "Integer", name = "id", value = "用户编号", 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("删除指定用户编号的用户") + @ApiImplicitParam(paramType = "query", dataTypeClass = Integer.class, name = "id", value = "用户编号", 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/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/UserController.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/UserController.java new file mode 100644 index 00000000..c7ec4107 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/controller/UserController.java @@ -0,0 +1,68 @@ +package cn.iocoder.springboot.lab24.apidoc.controller; + +import cn.iocoder.springboot.lab24.apidoc.dto.UserAddDTO; +import cn.iocoder.springboot.lab24.apidoc.dto.UserUpdateDTO; +import cn.iocoder.springboot.lab24.apidoc.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(paramType = "query", dataType = "Integer", name = "id", value = "用户编号", 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/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserAddDTO.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserAddDTO.java new file mode 100644 index 00000000..ad700e60 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserAddDTO.java @@ -0,0 +1,32 @@ +package cn.iocoder.springboot.lab24.apidoc.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/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserUpdateDTO.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserUpdateDTO.java new file mode 100644 index 00000000..054be806 --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/dto/UserUpdateDTO.java @@ -0,0 +1,43 @@ +package cn.iocoder.springboot.lab24.apidoc.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/src/main/java/cn/iocoder/springboot/lab24/apidoc/vo/UserVO.java b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/vo/UserVO.java new file mode 100644 index 00000000..2820594f --- /dev/null +++ b/lab-24/lab-24-apidoc-swagger/src/main/java/cn/iocoder/springboot/lab24/apidoc/vo/UserVO.java @@ -0,0 +1,32 @@ +package cn.iocoder.springboot.lab24.apidoc.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/pom.xml b/lab-24/pom.xml new file mode 100644 index 00000000..6dc43f34 --- /dev/null +++ b/lab-24/pom.xml @@ -0,0 +1,19 @@ + + + + labs-parent + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + lab-24 + pom + + lab-24-apidoc-swagger + + + + diff --git a/pom.xml b/pom.xml index ac6a6f05..51c2a11a 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,7 @@ lab-21 lab-22 lab-23 + lab-24