Explorar el Código

Error handling

Mitch Taylor hace 8 años
padre
commit
32a1238130

+ 12
- 0
src/main/java/dtos/error/ErrorDetail.java Ver fichero

1
 package dtos.error;
1
 package dtos.error;
2
 
2
 
3
+import java.util.List;
4
+import java.util.Map;
5
+
3
 public class ErrorDetail {
6
 public class ErrorDetail {
4
 
7
 
5
     private String title;
8
     private String title;
7
     private String detail;
10
     private String detail;
8
     private long timeStamp;
11
     private long timeStamp;
9
     private String developerMessage;
12
     private String developerMessage;
13
+    private Map<String, List<ValidationError>> errors;
10
 
14
 
11
     public String getTitle() {
15
     public String getTitle() {
12
         return title;
16
         return title;
48
         this.developerMessage = developerMessage;
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 Ver fichero

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 Ver fichero

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

+ 5
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/domain/Poll.java Ver fichero

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

+ 2
- 0
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Ver fichero

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