Skip to content

Commit ac4de4e

Browse files
kwoykepivovarit
authored andcommitted
BAEL-3576: Improve "Spring REST Entity to Dto" article (eugenp#8320)
* BAEL-3576: Update modelmapper to 2.3.5 * BAEL-3576: Code cleanup
1 parent ef0128e commit ac4de4e

3 files changed

Lines changed: 18 additions & 17 deletions

File tree

spring-boot-rest/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<start-class>com.baeldung.SpringBootRestApplication</start-class>
9090
<guava.version>27.0.1-jre</guava.version>
9191
<xstream.version>1.4.11.1</xstream.version>
92-
<modelmapper.version>2.3.3</modelmapper.version>
92+
<modelmapper.version>2.3.5</modelmapper.version>
9393
</properties>
9494

9595
</project>

spring-boot-rest/src/main/java/com/baeldung/modelmapper/controller/PostRestController.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package 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;
77
import org.modelmapper.ModelMapper;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.http.HttpStatus;
1010
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.GetMapping;
1112
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.PutMapping;
1215
import org.springframework.web.bind.annotation.RequestBody;
1316
import org.springframework.web.bind.annotation.RequestMapping;
14-
import org.springframework.web.bind.annotation.RequestMethod;
1517
import org.springframework.web.bind.annotation.ResponseBody;
1618
import 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);

spring-boot-rest/src/test/java/com/baeldung/modelmapper/PostDtoUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class PostDtoUnitTest {
1515
@Test
1616
public void whenConvertPostEntityToPostDto_thenCorrect() {
1717
Post post = new Post();
18-
post.setId(Long.valueOf(1));
18+
post.setId(1L);
1919
post.setTitle(randomAlphabetic(6));
2020
post.setUrl("www.test.com");
2121

@@ -28,7 +28,7 @@ public void whenConvertPostEntityToPostDto_thenCorrect() {
2828
@Test
2929
public void whenConvertPostDtoToPostEntity_thenCorrect() {
3030
PostDto postDto = new PostDto();
31-
postDto.setId(Long.valueOf(1));
31+
postDto.setId(1L);
3232
postDto.setTitle(randomAlphabetic(6));
3333
postDto.setUrl("www.test.com");
3434

0 commit comments

Comments
 (0)