Skip to content

Commit 390162a

Browse files
committed
Spring Boot 2.x基础教程:JSR-303实现请求参数校验
1 parent 3405c6f commit 390162a

File tree

7 files changed

+203
-0
lines changed

7 files changed

+203
-0
lines changed

chapter2-3/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

chapter2-3/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter2-3</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<name>chapter2-3</name>
17+
<description>Demo project for Spring Boot</description>
18+
19+
<properties>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.spring4all</groupId>
31+
<artifactId>swagger-spring-boot-starter</artifactId>
32+
<version>1.9.0.RELEASE</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.projectlombok</groupId>
37+
<artifactId>lombok</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace.chapter23;
2+
3+
import com.spring4all.swagger.EnableSwagger2Doc;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@EnableSwagger2Doc
8+
@SpringBootApplication
9+
public class Chapter23Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Chapter23Application.class, args);
13+
}
14+
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.didispace.chapter23;
2+
3+
import io.swagger.annotations.ApiModel;
4+
import io.swagger.annotations.ApiModelProperty;
5+
import lombok.Data;
6+
7+
import javax.validation.constraints.*;
8+
9+
@Data
10+
@ApiModel(description = "用户实体")
11+
public class User {
12+
13+
@ApiModelProperty("用户编号")
14+
private Long id;
15+
16+
@NotNull
17+
@Size(min = 2, max = 5)
18+
@ApiModelProperty("用户姓名")
19+
private String name;
20+
21+
@NotNull
22+
@Max(100)
23+
@Min(10)
24+
@ApiModelProperty("用户年龄")
25+
private Integer age;
26+
27+
@NotNull
28+
@Email
29+
@ApiModelProperty("用户邮箱")
30+
private String email;
31+
32+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.didispace.chapter23;
2+
3+
import io.swagger.annotations.Api;
4+
import io.swagger.annotations.ApiImplicitParam;
5+
import io.swagger.annotations.ApiOperation;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
import javax.validation.Valid;
9+
import java.util.*;
10+
11+
@Api(tags = "用户管理")
12+
@RestController
13+
@RequestMapping(value = "/users") // 通过这里配置使下面的映射都在/users下
14+
public class UserController {
15+
16+
// 创建线程安全的Map,模拟users信息的存储
17+
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<>());
18+
19+
@GetMapping("/")
20+
@ApiOperation(value = "获取用户列表")
21+
public List<User> getUserList() {
22+
List<User> r = new ArrayList<>(users.values());
23+
return r;
24+
}
25+
26+
@PostMapping("/")
27+
@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
28+
public String postUser(@Valid @RequestBody User user) {
29+
users.put(user.getId(), user);
30+
return "success";
31+
}
32+
33+
@GetMapping("/{id}")
34+
@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
35+
public User getUser(@PathVariable Long id) {
36+
return users.get(id);
37+
}
38+
39+
@PutMapping("/{id}")
40+
@ApiImplicitParam(paramType = "path", dataType = "Long", name = "id", value = "用户编号", required = true, example = "1")
41+
@ApiOperation(value = "更新用户详细信息", notes = "根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
42+
public String putUser(@PathVariable Long id, @RequestBody User user) {
43+
User u = users.get(id);
44+
u.setName(user.getName());
45+
u.setAge(user.getAge());
46+
users.put(id, u);
47+
return "success";
48+
}
49+
50+
@DeleteMapping("/{id}")
51+
@ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
52+
public String deleteUser(@PathVariable Long id) {
53+
users.remove(id);
54+
return "success";
55+
}
56+
57+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
swagger.title=spring-boot-starter-swagger
4+
swagger.description=Starter for swagger 2.x
5+
swagger.version=1.9.0.RELEASE
6+
swagger.license=Apache License, Version 2.0
7+
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
8+
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
9+
swagger.contact.name=didi
10+
swagger.contact.url=http://blog.didispace.com
11+
swagger.contact.email[email protected]
12+
swagger.base-package=com.didispace
13+
swagger.base-path=/**

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<!--Web开发-->
1616
<module>chapter2-1</module>
1717
<module>chapter2-2</module>
18+
<module>chapter2-3</module>
1819

1920

2021
</modules>

0 commit comments

Comments
 (0)