From 251f9f756e18a750de8417dee8a87371b7088bb1 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sun, 8 Dec 2019 01:25:43 +0100 Subject: [PATCH] BAEL-3576: Improve "Spring REST Entity to Dto" article (#8320) * BAEL-3576: Update modelmapper to 2.3.5 * BAEL-3576: Code cleanup --- spring-boot-rest/pom.xml | 2 +- .../controller/PostRestController.java | 41 ++++++++++--------- .../baeldung/modelmapper/PostDtoUnitTest.java | 4 +- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/spring-boot-rest/pom.xml b/spring-boot-rest/pom.xml index 57014381d8..3787e21493 100644 --- a/spring-boot-rest/pom.xml +++ b/spring-boot-rest/pom.xml @@ -89,7 +89,7 @@ com.baeldung.SpringBootRestApplication 27.0.1-jre 1.4.11.1 - 2.3.3 + 2.3.5 diff --git a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java index 68c17975d4..e2def62466 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java +++ b/spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java @@ -1,24 +1,25 @@ package com.baeldung.modelmapper.controller; -import java.text.ParseException; -import java.util.List; -import java.util.stream.Collectors; - -import org.modelmapper.ModelMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; - import com.baeldung.modelmapper.dto.PostDto; import com.baeldung.modelmapper.model.Post; import com.baeldung.modelmapper.service.IPostService; import com.baeldung.modelmapper.service.IUserService; +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import java.text.ParseException; +import java.util.List; +import java.util.stream.Collectors; @Controller @RequestMapping("/posts") @@ -33,7 +34,7 @@ public class PostRestController { @Autowired private ModelMapper modelMapper; - @RequestMapping(method = RequestMethod.GET) + @GetMapping @ResponseBody public List getPosts( @PathVariable("page") int page, @@ -43,11 +44,11 @@ public class PostRestController { List posts = postService.getPostsList(page, size, sortDir, sort); return posts.stream() - .map(post -> convertToDto(post)) + .map(this::convertToDto) .collect(Collectors.toList()); } - @RequestMapping(method = RequestMethod.POST) + @PostMapping @ResponseStatus(HttpStatus.CREATED) @ResponseBody public PostDto createPost(@RequestBody PostDto postDto) throws ParseException { @@ -56,13 +57,13 @@ public class PostRestController { return convertToDto(postCreated); } - @RequestMapping(value = "/{id}", method = RequestMethod.GET) + @GetMapping(value = "/{id}") @ResponseBody public PostDto getPost(@PathVariable("id") Long id) { return convertToDto(postService.getPostById(id)); } - @RequestMapping(value = "/{id}", method = RequestMethod.PUT) + @PutMapping(value = "/{id}") @ResponseStatus(HttpStatus.OK) public void updatePost(@RequestBody PostDto postDto) throws ParseException { Post post = convertToEntity(postDto); diff --git a/spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java b/spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java index 34ec4db783..6e94cc4289 100644 --- a/spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java +++ b/spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java @@ -15,7 +15,7 @@ public class PostDtoUnitTest { @Test public void whenConvertPostEntityToPostDto_thenCorrect() { Post post = new Post(); - post.setId(Long.valueOf(1)); + post.setId(1L); post.setTitle(randomAlphabetic(6)); post.setUrl("www.test.com"); @@ -28,7 +28,7 @@ public class PostDtoUnitTest { @Test public void whenConvertPostDtoToPostEntity_thenCorrect() { PostDto postDto = new PostDto(); - postDto.setId(Long.valueOf(1)); + postDto.setId(1L); postDto.setTitle(randomAlphabetic(6)); postDto.setUrl("www.test.com");