Skip to content

Commit

Permalink
knife4j
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Dec 2, 2020
1 parent a3d1ef6 commit 1541d6f
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 5 deletions.
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.github.chentianming11</groupId>
Expand Down Expand Up @@ -40,6 +40,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
Expand All @@ -50,6 +55,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索3.X最新版本号-->
<version>3.0.2</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//package com.github.chentianming11.spring.validation.base;
//
//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.spi.DocumentationType;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
//@Configuration
//@EnableSwagger2
//public class Swagger2Config {
//
//
// @Bean(value = "defaultApi2")
// public Docket defaultApi2() {
// Docket docket=new Docket(DocumentationType.SWAGGER_2)
// .apiInfo(new ApiInfoBuilder()
// .title("交易助手后台接口文档")
// .description("# helicarrier RESTful APIs")
// .termsOfServiceUrl("http://dora.shtest.ke.com/")
// .version("1.0")
// .build())
// //分组名称
// .select()
// //这里指定Controller扫描包路径
// .apis(RequestHandlerSelectors.basePackage("com.github.chentianming11.spring.validation"))
// .paths(PathSelectors.any())
// .build();
// return docket;
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.github.chentianming11.spring.validation.controller;

import lombok.Data;

/**
* @author 陈添明
*/
@Data
public class Person {

private Long id;

private String name;

private Integer age;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.chentianming11.spring.validation.controller;

import com.github.chentianming11.spring.validation.base.Result;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.*;

/**
* @author 陈添明
*/
@RestController
@RequestMapping("/api/person")
@Validated
public class PersonController {


@Data
public static class SavePerson {

private Long id;

@NotNull
@Size(min = 2, max = 10)
private String name;

@NotNull
@Max(200)
private Integer age;

@Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")
private String email;

}

@PostMapping("savePerson")
public Result savePerson(@RequestBody @Valid SavePerson savePerson) {
return Result.ok(savePerson);
}


@Data
public static class UpdatePerson {

@NotNull
@Min(1000)
@Max(10000000)
private Long id;

@Size(min = 2, max = 10)
private String name;

@Max(200)
private Integer age;

@Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$")
private String email;

}

@PostMapping("updatePerson")
public Result savePerson(@RequestBody @Valid UpdatePerson updatePerson) {
return Result.ok(updatePerson);
}



@GetMapping("queryPerson")
public Result queryPerson(@ApiParam(name = "用户id", example = "10") @NotNull @Min(1000) @Max(10000000) Long id,
@ApiParam(name = "姓名", example = "哈哈") @NotNull @Size(min = 2, max = 10) String name,
@Max(200) Integer age,
@Pattern(regexp = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$") String email) {


return Result.ok();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
import com.github.chentianming11.spring.validation.base.Validation.ValidationList;
import com.github.chentianming11.spring.validation.http.HttpTestAPI;
import com.github.chentianming11.spring.validation.pojo.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Set;
Expand All @@ -20,6 +24,7 @@
@RequestMapping("/api/user")
@RestController
@Validated
@Api("用户管理")
public class UserController {

@Autowired
Expand All @@ -30,7 +35,8 @@ public class UserController {

// 编程式校验
@PostMapping("/saveWithCodingValidate")
public Result saveWithCodingValidate(@RequestBody UserDTO userDTO) {
@ApiOperation("编程式校验保存")
public Result saveWithCodingValidate(@RequestBody @Valid UserDTO userDTO) {
Set<ConstraintViolation<UserDTO>> validate = globalValidator.validate(userDTO, UserDTO.Save.class);
// 如果校验通过,validate为空;否则,validate包含未校验通过项
if (validate.isEmpty()) {
Expand All @@ -46,18 +52,21 @@ public Result saveWithCodingValidate(@RequestBody UserDTO userDTO) {
}

@PostMapping("/save")
@ApiOperation("保存用户")
public Result saveUser(@RequestBody @Validated(UserDTO.Save.class) UserDTO userDTO) {
// 校验通过,才会执行业务逻辑处理
return Result.ok();
}

@PostMapping("/saveList")
@ApiOperation("批量保存")
public Result saveList(@RequestBody @Validated(UserDTO.Save.class) ValidationList<UserDTO> userList) {
// 校验通过,才会执行业务逻辑处理
return Result.ok();
}

@PostMapping("/update")
@ApiOperation("更新用户信息")
public Result updateUser(@RequestBody @Validated(UserDTO.Update.class) UserDTO userDTO) {
// 校验通过,才会执行业务逻辑处理
return Result.ok();
Expand All @@ -66,7 +75,8 @@ public Result updateUser(@RequestBody @Validated(UserDTO.Update.class) UserDTO u

// 路径变量
@GetMapping("{userId}")
public Result detail(@PathVariable("userId") @Min(10000000000000000L) Long userId) {
@ApiOperation("根据userId查询用户信息")
public Result detail(@PathVariable("userId") @Min(10000000000000000L) @ApiParam("用户id") Long userId) {
// 校验通过,才会执行业务逻辑处理
UserDTO userDTO = new UserDTO();
userDTO.setUserId(userId);
Expand All @@ -78,7 +88,8 @@ public Result detail(@PathVariable("userId") @Min(10000000000000000L) Long userI

// 查询参数
@GetMapping("getByAccount")
public Result getByAccount(@Length(min = 6, max = 20) @NotNull String account) {
@ApiOperation("根据account查询用户信息")
public Result getByAccount(@Length(min = 6, max = 20) @NotNull @ApiParam("账号") String account) {
// 校验通过,才会执行业务逻辑处理
UserDTO userDTO = new UserDTO();
userDTO.setUserId(10000000000000003L);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.chentianming11.spring.validation.pojo.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

Expand All @@ -11,37 +13,47 @@
* @author 陈添明
*/
@Data
@ApiModel("用户实体类")
public class UserDTO {

@ApiModelProperty("用户id")
@Min(value = 10000000000000000L, groups = Update.class)
private Long userId;

@ApiModelProperty("用户名")
@NotNull(groups = {Save.class, Update.class})
@Length(min = 2, max = 10, groups = {Save.class, Update.class})
private String userName;

@ApiModelProperty("账号")
@NotNull(groups = {Save.class, Update.class})
@Length(min = 6, max = 20, groups = {Save.class, Update.class})
private String account;

@ApiModelProperty("密码")
@NotNull(groups = {Save.class, Update.class})
@Length(min = 6, max = 20, groups = {Save.class, Update.class})
private String password;

@ApiModelProperty("工作")
@NotNull(groups = {Save.class, Update.class})
@Valid
private Job job;

@Data
@ApiModel("工作实体类")
public static class Job {

@ApiModelProperty("工作id")
@Min(value = 1, groups = Update.class)
private Long jobId;

@ApiModelProperty("工作名称")
@NotNull(groups = {Save.class, Update.class})
@Length(min = 2, max = 10, groups = {Save.class, Update.class})
private String jobName;

@ApiModelProperty("岗位")
@NotNull(groups = {Save.class, Update.class})
@Length(min = 2, max = 10, groups = {Save.class, Update.class})
private String position;
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ retrofit:
- com.github.lianjiatech.retrofit.spring.boot.core.ResponseCallAdapterFactory
enable-degrade: true
degrade-type: sentinel
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser
resource-name-parser: com.github.lianjiatech.retrofit.spring.boot.degrade.DefaultResourceNameParser


0 comments on commit 1541d6f

Please sign in to comment.