forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Boot配合Hibernate Validator参数校验
- Loading branch information
1 parent
88e80fc
commit 4a3a307
Showing
8 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.0.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.example</groupId> | ||
<artifactId>validator</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
46.Spring-Boot-Hibernate-Validator/src/main/java/com/example/demo/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...ng-Boot-Hibernate-Validator/src/main/java/com/example/demo/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.demo.controller; | ||
|
||
import com.example.demo.domain.User; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.validation.Valid; | ||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.NotBlank; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@RestController | ||
@Validated | ||
public class TestController { | ||
|
||
@GetMapping("test1") | ||
public String test1( | ||
@NotBlank(message = "{required}") String name, | ||
@Email(message = "{invalid}") String email) { | ||
return "success"; | ||
} | ||
|
||
@GetMapping("test2") | ||
public String test2(@Valid User user) { | ||
return "success"; | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
46.Spring-Boot-Hibernate-Validator/src/main/java/com/example/demo/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.example.demo.domain; | ||
|
||
import javax.validation.constraints.Email; | ||
import javax.validation.constraints.NotBlank; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
public class User implements Serializable { | ||
private static final long serialVersionUID = -2731598327208972274L; | ||
|
||
@NotBlank(message = "{required}") | ||
private String name; | ||
|
||
@Email(message = "{invalid}") | ||
private String email; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ot-Hibernate-Validator/src/main/java/com/example/demo/handler/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.example.demo.handler; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.validation.BindException; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.ConstraintViolationException; | ||
import javax.validation.Path; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author MrBird | ||
*/ | ||
@RestControllerAdvice | ||
@Order(value = Ordered.HIGHEST_PRECEDENCE) | ||
public class GlobalExceptionHandler { | ||
|
||
/** | ||
* 统一处理请求参数校验(普通传参) | ||
* | ||
* @param e ConstraintViolationException | ||
* @return FebsResponse | ||
*/ | ||
@ExceptionHandler(value = ConstraintViolationException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public String handleConstraintViolationException(ConstraintViolationException e) { | ||
StringBuilder message = new StringBuilder(); | ||
Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); | ||
for (ConstraintViolation<?> violation : violations) { | ||
Path path = violation.getPropertyPath(); | ||
String[] pathArr = StringUtils.splitByWholeSeparatorPreserveAllTokens(path.toString(), "."); | ||
message.append(pathArr[1]).append(violation.getMessage()).append(","); | ||
} | ||
message = new StringBuilder(message.substring(0, message.length() - 1)); | ||
return message.toString(); | ||
} | ||
|
||
/** | ||
* 统一处理请求参数校验(实体对象传参) | ||
* | ||
* @param e BindException | ||
* @return FebsResponse | ||
*/ | ||
@ExceptionHandler(BindException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
public String validExceptionHandler(BindException e) { | ||
StringBuilder message = new StringBuilder(); | ||
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors(); | ||
for (FieldError error : fieldErrors) { | ||
message.append(error.getField()).append(error.getDefaultMessage()).append(","); | ||
} | ||
message = new StringBuilder(message.substring(0, message.length() - 1)); | ||
return message.toString(); | ||
|
||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
46.Spring-Boot-Hibernate-Validator/src/main/resources/ValidationMessages.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
required=\u4e0d\u80fd\u4e3a\u7a7a | ||
invalid=\u683c\u5f0f\u4e0d\u5408\u6cd5 |
1 change: 1 addition & 0 deletions
1
46.Spring-Boot-Hibernate-Validator/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
16 changes: 16 additions & 0 deletions
16
46.Spring-Boot-Hibernate-Validator/src/test/java/com/example/demo/DemoApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.example.demo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class DemoApplicationTests { | ||
|
||
@Test | ||
public void contextLoads() { | ||
} | ||
|
||
} |