Skip to content

Commit 3d98109

Browse files
committed
validate模块
1 parent 8c8da02 commit 3d98109

File tree

14 files changed

+328
-4
lines changed

14 files changed

+328
-4
lines changed

common/src/main/java/name/guolanren/exception/GlobalExceptionHandler.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import name.guolanren.http.response.ResultEntity;
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
7-
import org.springframework.web.bind.annotation.ControllerAdvice;
87
import org.springframework.web.bind.annotation.ExceptionHandler;
9-
import org.springframework.web.bind.annotation.ResponseBody;
8+
import org.springframework.web.bind.annotation.RestController;
109

1110
/**
1211
* @author guolanren
1312
*/
14-
@ControllerAdvice
15-
@ResponseBody
13+
@RestController
1614
public class GlobalExceptionHandler {
1715

1816
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

validate/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Spring Boot Validate
2+
3+
## 概述
4+
5+
Spring Boot Validate 概述...

validate/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.springframework.boot</groupId>
9+
<artifactId>spring-boot-starter-parent</artifactId>
10+
<version>2.1.6.RELEASE</version>
11+
<relativePath/> <!-- lookup parent from repository -->
12+
</parent>
13+
14+
<groupId>name.guolanren</groupId>
15+
<artifactId>validate</artifactId>
16+
<version>1.0.0</version>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-test</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-maven-plugin</artifactId>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package name.guolanren;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author guolanren
8+
*/
9+
@SpringBootApplication
10+
public class ValidateApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(ValidateApplication.class, args);
14+
}
15+
16+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package name.guolanren.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.validation.BindingResult;
7+
import org.springframework.validation.FieldError;
8+
import org.springframework.validation.ObjectError;
9+
import org.springframework.web.bind.MethodArgumentNotValidException;
10+
import org.springframework.web.bind.MissingServletRequestParameterException;
11+
import org.springframework.web.bind.annotation.ExceptionHandler;
12+
import org.springframework.web.bind.annotation.RestControllerAdvice;
13+
14+
import javax.validation.ConstraintViolationException;
15+
import java.util.Arrays;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
/**
20+
* @author guolanren
21+
*/
22+
@RestControllerAdvice
23+
public class GlobalExceptionHandler {
24+
25+
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
26+
27+
/**
28+
* JSR 303: org.springframework.web.bind.MethodArgumentNotValidException
29+
*/
30+
@ExceptionHandler
31+
public ResponseEntity<String> methodArgumentNotValidHandle(MethodArgumentNotValidException e) {
32+
BindingResult bs = e.getBindingResult();
33+
Map<String, String> errors = new HashMap<>(8);
34+
for (ObjectError objectError : bs.getAllErrors()) {
35+
FieldError fieldError = (FieldError) objectError;
36+
errors.put(fieldError.getField(), fieldError.getDefaultMessage());
37+
}
38+
return ResponseEntity.ok(errors.toString());
39+
}
40+
41+
/**
42+
* JSR-303: javax.validation.ConstraintViolationException
43+
* Spring MVC: org.springframework.web.bind.MissingServletRequestParameterException
44+
*/
45+
@ExceptionHandler({ConstraintViolationException.class, MissingServletRequestParameterException.class})
46+
public ResponseEntity<String> constraintViolationHandle(Exception e) {
47+
return ResponseEntity.ok(e.getMessage());
48+
}
49+
50+
@ExceptionHandler
51+
public ResponseEntity<String> unknownHandle(Exception e) {
52+
LOG.error(e.getMessage(), e);
53+
return ResponseEntity.ok(e.getMessage());
54+
}
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package name.guolanren.controller;
2+
3+
import name.guolanren.group.CreateValidationGroup;
4+
import name.guolanren.group.ModifyValidationGroup;
5+
import name.guolanren.group.SearchValidationGroup;
6+
import name.guolanren.model.User;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.*;
10+
11+
import javax.validation.constraints.NotBlank;
12+
import javax.validation.constraints.NotNull;
13+
import javax.validation.constraints.Positive;
14+
import javax.validation.groups.Default;
15+
16+
/**
17+
* @author guolanren
18+
*/
19+
@RestController
20+
@Validated
21+
public class ValidateController {
22+
23+
@GetMapping("/search")
24+
public ResponseEntity<String> search(@NotBlank String name) {
25+
return ResponseEntity.ok("success");
26+
}
27+
28+
@PostMapping("/add")
29+
public ResponseEntity<String> add(@RequestBody
30+
@Validated(value = {CreateValidationGroup.class, Default.class}) User user) {
31+
return ResponseEntity.ok("success");
32+
}
33+
34+
@PutMapping("/modify")
35+
public ResponseEntity<String> modify(@RequestBody
36+
@Validated(value = {ModifyValidationGroup.class, Default.class}) User user) {
37+
return ResponseEntity.ok("success");
38+
}
39+
40+
@DeleteMapping("/delete")
41+
public ResponseEntity<String> delete(@NotNull @Positive Long id) {
42+
return ResponseEntity.ok("success");
43+
}
44+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package name.guolanren.group;
2+
3+
/**
4+
* @author guolanren
5+
*/
6+
public interface CreateValidationGroup {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package name.guolanren.group;
2+
3+
/**
4+
* @author guolanren
5+
*/
6+
public interface DeleteValidationGroup {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package name.guolanren.group;
2+
3+
/**
4+
* @author guolanren
5+
*/
6+
public interface ModifyValidationGroup {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package name.guolanren.group;
2+
3+
/**
4+
* @author guolanren
5+
*/
6+
public interface SearchValidationGroup {
7+
}

0 commit comments

Comments
 (0)