11package com .baeldung .modelmapper .controller ;
22
3- import java . text . ParseException ;
4- import java . util . List ;
5- import java . util . stream . Collectors ;
6-
3+ import com . baeldung . modelmapper . dto . PostDto ;
4+ import com . baeldung . modelmapper . model . Post ;
5+ import com . baeldung . modelmapper . service . IPostService ;
6+ import com . baeldung . modelmapper . service . IUserService ;
77import org .modelmapper .ModelMapper ;
88import org .springframework .beans .factory .annotation .Autowired ;
99import org .springframework .http .HttpStatus ;
1010import org .springframework .stereotype .Controller ;
11+ import org .springframework .web .bind .annotation .GetMapping ;
1112import org .springframework .web .bind .annotation .PathVariable ;
13+ import org .springframework .web .bind .annotation .PostMapping ;
14+ import org .springframework .web .bind .annotation .PutMapping ;
1215import org .springframework .web .bind .annotation .RequestBody ;
1316import org .springframework .web .bind .annotation .RequestMapping ;
14- import org .springframework .web .bind .annotation .RequestMethod ;
1517import org .springframework .web .bind .annotation .ResponseBody ;
1618import org .springframework .web .bind .annotation .ResponseStatus ;
1719
18- import com .baeldung .modelmapper .dto .PostDto ;
19- import com .baeldung .modelmapper .model .Post ;
20- import com .baeldung .modelmapper .service .IPostService ;
21- import com .baeldung .modelmapper .service .IUserService ;
20+ import java .text .ParseException ;
21+ import java .util .List ;
22+ import java .util .stream .Collectors ;
2223
2324@ Controller
2425@ RequestMapping ("/posts" )
@@ -33,7 +34,7 @@ public class PostRestController {
3334 @ Autowired
3435 private ModelMapper modelMapper ;
3536
36- @ RequestMapping ( method = RequestMethod . GET )
37+ @ GetMapping
3738 @ ResponseBody
3839 public List <PostDto > getPosts (
3940 @ PathVariable ("page" ) int page ,
@@ -43,11 +44,11 @@ public List<PostDto> getPosts(
4344
4445 List <Post > posts = postService .getPostsList (page , size , sortDir , sort );
4546 return posts .stream ()
46- .map (post -> convertToDto ( post ) )
47+ .map (this :: convertToDto )
4748 .collect (Collectors .toList ());
4849 }
4950
50- @ RequestMapping ( method = RequestMethod . POST )
51+ @ PostMapping
5152 @ ResponseStatus (HttpStatus .CREATED )
5253 @ ResponseBody
5354 public PostDto createPost (@ RequestBody PostDto postDto ) throws ParseException {
@@ -56,13 +57,13 @@ public PostDto createPost(@RequestBody PostDto postDto) throws ParseException {
5657 return convertToDto (postCreated );
5758 }
5859
59- @ RequestMapping (value = "/{id}" , method = RequestMethod . GET )
60+ @ GetMapping (value = "/{id}" )
6061 @ ResponseBody
6162 public PostDto getPost (@ PathVariable ("id" ) Long id ) {
6263 return convertToDto (postService .getPostById (id ));
6364 }
6465
65- @ RequestMapping (value = "/{id}" , method = RequestMethod . PUT )
66+ @ PutMapping (value = "/{id}" )
6667 @ ResponseStatus (HttpStatus .OK )
6768 public void updatePost (@ RequestBody PostDto postDto ) throws ParseException {
6869 Post post = convertToEntity (postDto );
0 commit comments