Browse Source

validationerror

Seth 5 years ago
parent
commit
1171deb5cb

+ 3
- 2
src/main/java/io/zipcoder/tc_spring_poll_application/controller/PollController.java View File

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

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java View File

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

+ 12
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/dtos/error/ErrorDetail.java View File

1
 package io.zipcoder.tc_spring_poll_application.dtos.error;
1
 package io.zipcoder.tc_spring_poll_application.dtos.error;
2
 
2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+
3
 public class ErrorDetail {
6
 public class ErrorDetail {
4
     private String title;
7
     private String title;
5
     private int status;
8
     private int status;
6
     private String detail;
9
     private String detail;
7
     private long timestamp;
10
     private long timestamp;
8
     private String developerMessage;
11
     private String developerMessage;
12
+    private Map<String, List<ValidationError>> errors;
9
 
13
 
10
     public ErrorDetail(String title, int status, String detail, long timestamp, String developerMessage) {
14
     public ErrorDetail(String title, int status, String detail, long timestamp, String developerMessage) {
11
         this.title = title;
15
         this.title = title;
57
     public void setDeveloperMessage(String developerMessage) {
61
     public void setDeveloperMessage(String developerMessage) {
58
         this.developerMessage = developerMessage;
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 View File

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
+}