Przeglądaj źródła

validationerror

Seth 5 lat temu
rodzic
commit
1171deb5cb

+ 3
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Wyświetl plik

@@ -10,6 +10,7 @@ import org.springframework.http.ResponseEntity;
10 10
 import org.springframework.web.bind.annotation.*;
11 11
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
12 12
 
13
+import javax.validation.Valid;
13 14
 import java.net.URI;
14 15
 
15 16
 @RestController
@@ -29,7 +30,7 @@ public class PollController {
29 30
     }
30 31
 
31 32
     @RequestMapping (method=RequestMethod.POST)
32
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
33
+    public ResponseEntity<?> createPoll(@RequestBody @Valid Poll poll) {
33 34
         URI newPollUri = ServletUriComponentsBuilder
34 35
                 .fromCurrentRequest()
35 36
                 .path("/{id}")
@@ -49,7 +50,7 @@ public class PollController {
49 50
     }
50 51
 
51 52
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
52
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
53
+    public ResponseEntity<?> updatePoll(@RequestBody @Valid Poll poll, @PathVariable Long pollId) {
53 54
         // Save the entity
54 55
         verifyPoll(pollId);
55 56
         Poll p = pollRepository.save(poll);

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Wyświetl plik

@@ -1,6 +1,9 @@
1 1
 package io.zipcoder.tc_spring_poll_application.domain;
2 2
 
3
+import org.hibernate.validator.constraints.NotEmpty;
4
+
3 5
 import javax.persistence.*;
6
+import javax.validation.constraints.Size;
4 7
 import java.util.Set;
5 8
 
6 9
 @Entity
@@ -12,11 +15,13 @@ public class Poll {
12 15
     private long id;
13 16
 
14 17
     @Column(name = "QUESTION")
18
+    @NotEmpty
15 19
     private String question;
16 20
 
17 21
     @OneToMany(cascade = CascadeType.ALL)
18 22
     @JoinColumn(name = "POLL_ID")
19 23
     @OrderBy
24
+    @Size(min = 2, max = 6)
20 25
     private Set<Option> options;
21 26
 
22 27
 

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java Wyświetl plik

@@ -1,11 +1,15 @@
1 1
 package io.zipcoder.tc_spring_poll_application.dtos.error;
2 2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+
3 6
 public class ErrorDetail {
4 7
     private String title;
5 8
     private int status;
6 9
     private String detail;
7 10
     private long timestamp;
8 11
     private String developerMessage;
12
+    private Map<String, List<ValidationError>> errors;
9 13
 
10 14
     public ErrorDetail(String title, int status, String detail, long timestamp, String developerMessage) {
11 15
         this.title = title;
@@ -57,4 +61,12 @@ public class ErrorDetail {
57 61
     public void setDeveloperMessage(String developerMessage) {
58 62
         this.developerMessage = developerMessage;
59 63
     }
64
+
65
+    public Map<String, List<ValidationError>> getErrors() {
66
+        return errors;
67
+    }
68
+
69
+    public void setErrors(Map<String, List<ValidationError>> errors) {
70
+        this.errors = errors;
71
+    }
60 72
 }

+ 25
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ValidationError.java Wyświetl plik

@@ -0,0 +1,25 @@
1
+package io.zipcoder.tc_spring_poll_application.dtos.error;
2
+
3
+public class ValidationError {
4
+    private String code;
5
+    private String message;
6
+
7
+    public ValidationError() {
8
+    }
9
+
10
+    public String getCode() {
11
+        return code;
12
+    }
13
+
14
+    public void setCode(String code) {
15
+        this.code = code;
16
+    }
17
+
18
+    public String getMessage() {
19
+        return message;
20
+    }
21
+
22
+    public void setMessage(String message) {
23
+        this.message = message;
24
+    }
25
+}