Bladeren bron

Up to and including part 5 completed

PeterMcCormick 8 jaren geleden
bovenliggende
commit
c8c2e4a886

+ 13
- 7
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Bestand weergeven

2
 
2
 
3
 import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
3
 import io.zipcoder.tc_spring_poll_application.error.ErrorDetail;
4
 import io.zipcoder.tc_spring_poll_application.error.ValidationError;
4
 import io.zipcoder.tc_spring_poll_application.error.ValidationError;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.context.MessageSource;
5
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.HttpStatus;
6
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.http.ResponseEntity;
7
 import org.springframework.validation.FieldError;
9
 import org.springframework.validation.FieldError;
8
 import org.springframework.web.bind.MethodArgumentNotValidException;
10
 import org.springframework.web.bind.MethodArgumentNotValidException;
9
-import org.springframework.web.bind.annotation.ControllerAdvice;
10
-import org.springframework.web.bind.annotation.ExceptionHandler;
11
+import org.springframework.web.bind.annotation.*;
11
 import sun.tools.tree.FieldExpression;
12
 import sun.tools.tree.FieldExpression;
12
 
13
 
13
 import javax.servlet.http.HttpServletRequest;
14
 import javax.servlet.http.HttpServletRequest;
19
 @ControllerAdvice
20
 @ControllerAdvice
20
 public class RestExceptionHandler {
21
 public class RestExceptionHandler {
21
 
22
 
23
+    @Autowired
24
+    private MessageSource messageSource;
25
+
22
     //    @ExceptionHandler(ResourceNotFoundException.class)
26
     //    @ExceptionHandler(ResourceNotFoundException.class)
23
 //    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
27
 //    public ResponseEntity<?> handleResourceNotFoundException(ResourceNotFoundException rnfe, HttpServletRequest request) {
24
 //        ErrorDetail errorDetail = new ErrorDetail();
28
 //        ErrorDetail errorDetail = new ErrorDetail();
30
 //        return  new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
34
 //        return  new ResponseEntity<>(errorDetail, null, HttpStatus.NOT_FOUND);
31
 //        }
35
 //        }
32
     @ExceptionHandler(MethodArgumentNotValidException.class)
36
     @ExceptionHandler(MethodArgumentNotValidException.class)
33
-    public ResponseEntity<?>
34
-    handleValidationError(MethodArgumentNotValidException manve, HttpServletRequest request) {
37
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
38
+    public @ResponseBody ErrorDetail handleValidationError(MethodArgumentNotValidException
39
+        manve, HttpServletRequest request) {
35
 
40
 
36
         ErrorDetail errorDetail = new ErrorDetail();
41
         ErrorDetail errorDetail = new ErrorDetail();
37
         // Populate errorDetail instance
42
         // Populate errorDetail instance
50
             List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
55
             List<ValidationError> validationErrorList = errorDetail.getErrors().get(fe.getField());
51
             if (validationErrorList == null) {
56
             if (validationErrorList == null) {
52
                 validationErrorList = new ArrayList<ValidationError>();
57
                 validationErrorList = new ArrayList<ValidationError>();
53
-                errorDetail.getErrors().put(fe.getField(), validationErrorList);
58
+                errorDetail.getErrors().put(fe.getField(),
59
+                    validationErrorList);
54
             }
60
             }
55
             ValidationError validationError = new ValidationError();
61
             ValidationError validationError = new ValidationError();
56
             validationError.setCode(fe.getCode());
62
             validationError.setCode(fe.getCode());
57
-            validationError.setMessage(fe.getDefaultMessage());
63
+            validationError.setMessage(messageSource.getMessage(fe,null));
58
             validationErrorList.add(validationError);
64
             validationErrorList.add(validationError);
59
         }
65
         }
60
-        return new ResponseEntity<>(errorDetail, null, HttpStatus.BAD_REQUEST);
66
+        return errorDetail;
61
 
67
 
62
     }
68
     }
63
     /** handleResourceNotFoundException method removed **/
69
     /** handleResourceNotFoundException method removed **/

+ 2
- 0
src/resources/messages.properties Bestand weergeven

1
+NotEmpty.poll.question=Question is a required field
2
+Size.poll.options=Options must be greater than {2} and less than {1}