Mitch Taylor vor 8 Jahren
Ursprung
Commit
32a1238130

+ 12
- 0
src/main/java/dtos/error/ErrorDetail.java Datei anzeigen

@@ -1,5 +1,8 @@
1 1
 package dtos.error;
2 2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+
3 6
 public class ErrorDetail {
4 7
 
5 8
     private String title;
@@ -7,6 +10,7 @@ public class ErrorDetail {
7 10
     private String detail;
8 11
     private long timeStamp;
9 12
     private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
10 14
 
11 15
     public String getTitle() {
12 16
         return title;
@@ -48,4 +52,12 @@ public class ErrorDetail {
48 52
         this.developerMessage = developerMessage;
49 53
     }
50 54
 
55
+    public Map<String, List<ValidationError>> getErrors() {
56
+        return errors;
57
+    }
58
+
59
+    public void setErrors(Map<String, List<ValidationError>> errors) {
60
+        this.errors = errors;
61
+    }
62
+
51 63
 }

+ 24
- 0
src/main/java/dtos/error/ValidationError.java Datei anzeigen

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

+ 3
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java Datei anzeigen

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
10 10
 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
11 11
 
12 12
 import javax.inject.Inject;
13
+import javax.validation.Valid;
13 14
 import java.net.URI;
14 15
 
15 16
 @RestController
@@ -32,7 +33,7 @@ public class PollController {
32 33
     }
33 34
 
34 35
     @RequestMapping(value="/polls", method=RequestMethod.POST)
35
-    public ResponseEntity<?> createPoll(@RequestBody Poll poll) {
36
+    public ResponseEntity<?> createPoll(@Valid @RequestBody Poll poll) {
36 37
         poll = pollRepository.save(poll);
37 38
         URI newPollUri = ServletUriComponentsBuilder
38 39
                 .fromCurrentRequest()
@@ -45,7 +46,7 @@ public class PollController {
45 46
     }
46 47
 
47 48
     @RequestMapping(value="/polls/{pollId}", method=RequestMethod.PUT)
48
-    public ResponseEntity<?> updatePoll(@RequestBody Poll poll, @PathVariable Long pollId) {
49
+    public ResponseEntity<?> updatePoll(@Valid @RequestBody Poll poll, @PathVariable Long pollId) {
49 50
         verifyPoll(pollId);
50 51
         Poll p = pollRepository.save(poll);
51 52
         return new ResponseEntity<>(HttpStatus.OK);

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Datei anzeigen

@@ -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
@@ -11,12 +14,14 @@ public class Poll {
11 14
     @Column(name = "POLL_ID")
12 15
     private Long id;
13 16
 
17
+    @NotEmpty
14 18
     @Column(name = "QUESTION")
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
     public Long getId() {

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Datei anzeigen

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
10 10
 
11 11
 @ControllerAdvice
12 12
 public class RestExceptionHandler {
13
+
13 14
     @ExceptionHandler(ResourceNotFoundException.class)
14 15
     public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
15 16
         ErrorDetail errorDetail = new ErrorDetail();
@@ -20,4 +21,5 @@ public class RestExceptionHandler {
20 21
         errorDetail.setDeveloperMessage(rnfe.getClass().getName());
21 22
         return new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
22 23
     }
24
+
23 25
 }