File tree Expand file tree Collapse file tree 6 files changed +143
-0
lines changed
Expand file tree Collapse file tree 6 files changed +143
-0
lines changed Original file line number Diff line number Diff line change 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
4+ <modelVersion >4.0.0</modelVersion >
5+
6+ <parent >
7+ <groupId >com.jonssonyan</groupId >
8+ <artifactId >spring-demo</artifactId >
9+ <version >0.0.1-SNAPSHOT</version >
10+ <relativePath >../pom.xml</relativePath > <!-- lookup parent from repository -->
11+ </parent >
12+
13+ <artifactId >spring-boot-validation</artifactId >
14+ <version >0.0.1-SNAPSHOT</version >
15+ <name >springboot-validation</name >
16+ <description >Demo project for Spring Boot Validation</description >
17+
18+ <properties >
19+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
20+ <encoding >UTF-8</encoding >
21+ <java .version>8</java .version>
22+ <maven .compiler.source>${java.version} </maven .compiler.source>
23+ <maven .compiler.target>${java.version} </maven .compiler.target>
24+ </properties >
25+
26+ <dependencies >
27+ <dependency >
28+ <groupId >org.springframework.boot</groupId >
29+ <artifactId >spring-boot-starter-web</artifactId >
30+ </dependency >
31+ <dependency >
32+ <groupId >org.springframework.boot</groupId >
33+ <artifactId >spring-boot-starter-validation</artifactId >
34+ </dependency >
35+ </dependencies >
36+
37+ <build >
38+ <plugins >
39+ <plugin >
40+ <groupId >org.springframework.boot</groupId >
41+ <artifactId >spring-boot-maven-plugin</artifactId >
42+ </plugin >
43+ </plugins >
44+ </build >
45+ </project >
Original file line number Diff line number Diff line change 1+ package com .jonssonyan ;
2+
3+ import com .jonssonyan .model .dto .ReqDto ;
4+ import org .springframework .boot .SpringApplication ;
5+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6+ import org .springframework .web .bind .annotation .PostMapping ;
7+ import org .springframework .web .bind .annotation .RequestBody ;
8+ import org .springframework .web .bind .annotation .RestController ;
9+
10+ import javax .validation .Valid ;
11+
12+ @ SpringBootApplication
13+ @ RestController
14+ public class SpringBootValidationApplication {
15+
16+ public static void main (String [] args ) {
17+ SpringApplication .run (SpringBootValidationApplication .class , args );
18+ }
19+
20+ @ PostMapping ("signUp" )
21+ private String signUp (@ RequestBody @ Valid ReqDto dto ) {
22+ return "success" ;
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1+ package com .jonssonyan .config ;
2+
3+ import org .springframework .validation .FieldError ;
4+ import org .springframework .web .bind .MethodArgumentNotValidException ;
5+ import org .springframework .web .bind .annotation .ExceptionHandler ;
6+ import org .springframework .web .bind .annotation .RestControllerAdvice ;
7+
8+ import java .util .List ;
9+
10+ @ RestControllerAdvice
11+ public class GlobalExceptionHandler {
12+
13+ @ ExceptionHandler (MethodArgumentNotValidException .class )
14+ public String handleValidationException (MethodArgumentNotValidException e ) {
15+ List <FieldError > errors = e .getBindingResult ().getFieldErrors ();
16+ StringBuilder errorMessage = new StringBuilder ("参数错误:" );
17+ for (FieldError error : errors ) {
18+ errorMessage .append (error .getField ())
19+ .append (" - " )
20+ .append (error .getDefaultMessage ())
21+ .append ("; " );
22+ }
23+ return errorMessage .toString ();
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package com .jonssonyan .model .dto ;
2+
3+ import lombok .Data ;
4+
5+ import javax .validation .constraints .Email ;
6+ import javax .validation .constraints .Max ;
7+ import javax .validation .constraints .Min ;
8+ import javax .validation .constraints .NotBlank ;
9+ import javax .validation .constraints .NotEmpty ;
10+ import javax .validation .constraints .Past ;
11+ import javax .validation .constraints .Size ;
12+ import java .time .LocalDate ;
13+ import java .util .List ;
14+
15+ @ Data
16+ public class ReqDto {
17+ @ NotBlank (message = "用户名不能为空" )
18+ @ Size (min = 6 , max = 20 , message = "用户名长度必须在6到20之间" )
19+ private String username ;
20+ @ NotBlank (message = "密码不能为空" )
21+ @ Size (min = 6 , max = 20 , message = "密码长度必须在6到20之间" )
22+ private String password ;
23+ @ Email (message = "请输入有效的邮箱地址" )
24+ private String email ;
25+ @ Max (value = 200 , message = "年龄必须小于200" )
26+ @ Min (value = 0 , message = "年龄必须大于0" )
27+ private Integer age ;
28+ @ NotEmpty (message = "教育经历不能为空" )
29+ private List <String > education ;
30+ @ Past (message = "出生日期必须是过去的日期" )
31+ private LocalDate birthDate ;
32+ }
Original file line number Diff line number Diff line change 1+ server.port =9036
2+ server.servlet.context-path =/validation
3+ spring.application.name =spring-boot-validation
Original file line number Diff line number Diff line change 1+ package com .jonssonyan ;
2+
3+ import org .junit .jupiter .api .Test ;
4+ import org .springframework .boot .test .context .SpringBootTest ;
5+
6+ @ SpringBootTest
7+ class SpringBootValidationApplicationTests {
8+
9+ @ Test
10+ void contextLoads () {
11+ }
12+
13+ }
You can’t perform that action at this time.
0 commit comments