Просмотр исходного кода

Up to and including part 5 completed

PeterMcCormick 8 лет назад
Родитель
Сommit
c8c2e4a886

+ 13
- 7
src/main/java/io/zipcoder/tc_spring_poll_application/exception/RestExceptionHandler.java Просмотреть файл

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

+ 2
- 0
src/resources/messages.properties Просмотреть файл

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