BAEL-3576: Improve "Spring REST Entity to Dto" article (#8320)

* BAEL-3576: Update modelmapper to 2.3.5

* BAEL-3576: Code cleanup
This commit is contained in:
kwoyke 2019-12-08 01:25:43 +01:00 committed by Grzegorz Piwowarek
parent d0a9b57694
commit 251f9f756e
3 changed files with 24 additions and 23 deletions

View File

@ -89,7 +89,7 @@
<start-class>com.baeldung.SpringBootRestApplication</start-class>
<guava.version>27.0.1-jre</guava.version>
<xstream.version>1.4.11.1</xstream.version>
<modelmapper.version>2.3.3</modelmapper.version>
<modelmapper.version>2.3.5</modelmapper.version>
</properties>
</project>

View File

@ -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<PostDto> getPosts(
@PathVariable("page") int page,
@ -43,11 +44,11 @@ public class PostRestController {
List<Post> 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);

View File

@ -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");