|
2 | 2 |
|
3 | 3 | import com.hantsylabs.restexample.springmvc.Constants;
|
4 | 4 | import com.hantsylabs.restexample.springmvc.domain.Post;
|
| 5 | +import com.hantsylabs.restexample.springmvc.exception.InvalidRequestException; |
5 | 6 | import com.hantsylabs.restexample.springmvc.model.CommentDetails;
|
6 | 7 | import com.hantsylabs.restexample.springmvc.model.CommentForm;
|
7 | 8 | import com.hantsylabs.restexample.springmvc.model.PostDetails;
|
8 | 9 | import com.hantsylabs.restexample.springmvc.model.PostForm;
|
9 | 10 | import com.hantsylabs.restexample.springmvc.model.ResponseMessage;
|
10 | 11 | import com.hantsylabs.restexample.springmvc.service.BlogService;
|
11 | 12 | import javax.inject.Inject;
|
| 13 | +import javax.validation.Valid; |
12 | 14 | import org.slf4j.Logger;
|
13 | 15 | import org.slf4j.LoggerFactory;
|
14 | 16 | import org.springframework.data.domain.Page;
|
|
18 | 20 | import org.springframework.http.HttpHeaders;
|
19 | 21 | import org.springframework.http.HttpStatus;
|
20 | 22 | import org.springframework.http.ResponseEntity;
|
| 23 | +import org.springframework.validation.BindingResult; |
21 | 24 | import org.springframework.web.bind.annotation.PathVariable;
|
22 | 25 | import org.springframework.web.bind.annotation.RequestBody;
|
23 | 26 | import org.springframework.web.bind.annotation.RequestMapping;
|
@@ -87,9 +90,12 @@ public ResponseEntity<Page<CommentDetails>> getCommentsOfPost(
|
87 | 90 |
|
88 | 91 | @RequestMapping(value = "", method = RequestMethod.POST)
|
89 | 92 | @ResponseBody
|
90 |
| - public ResponseEntity<ResponseMessage> createPost(@RequestBody PostForm post) { |
| 93 | + public ResponseEntity<ResponseMessage> createPost(@RequestBody @Valid PostForm post, BindingResult errResult) { |
91 | 94 |
|
92 | 95 | log.debug("create a new post");
|
| 96 | + if (errResult.hasErrors()) { |
| 97 | + throw new InvalidRequestException(errResult); |
| 98 | + } |
93 | 99 |
|
94 | 100 | PostDetails saved = blogService.savePost(post);
|
95 | 101 |
|
|
0 commit comments