Skip to content

Commit a813d8d

Browse files
author
frankyoh
committed
added modelmapper example
1 parent 2539323 commit a813d8d

88 files changed

Lines changed: 4202 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
<!--<module>springboot-security-junit-test-withmockuser</module>-->
9898
<module>springboot-security</module>
9999
<module>springboot-data-jpa-h2</module>
100-
<!--? <module>springboot-modelmapper</module>-->
100+
<module>springboot-modelmapper</module>
101101
<module>springboot-resttemplate</module>
102102
<module>springboot-quartz-cluster</module>
103103
<module>springboot-quartz-reactjs-angularjs</module>

springboot-modelmapper/.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/

springboot-modelmapper/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Module for the articles that are part of the Spring REST E-book:
2+
3+
1. [Bootstrap a Web Application with Spring 5](https://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration)
4+
2. [Build a REST API with Spring and Java Config](http://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration)
5+
3. [Http Message Converters with the Spring Framework](http://www.baeldung.com/spring-httpmessageconverter-rest)
6+
4. [Spring’s RequestBody and ResponseBody Annotations](https://www.baeldung.com/spring-request-response-body)
7+
5. [Entity To DTO Conversion for a Spring REST API](https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application)
8+
6. [Error Handling for REST with Spring](http://www.baeldung.com/exception-handling-for-rest-with-spring)
9+
7. [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability)
10+
8. [An Intro to Spring HATEOAS](http://www.baeldung.com/spring-hateoas-tutorial)
11+
9. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
12+
10. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
13+
14+
- [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
15+
- [Versioning a REST API](http://www.baeldung.com/rest-versioning)
16+
- [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
17+
- [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
18+
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
19+
- [Spring Boot Consuming and Producing JSON](https://www.baeldung.com/spring-boot-json)

springboot-modelmapper/pom.xml

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.advenoh</groupId>
14+
<artifactId>springboot-modelmapper</artifactId>
15+
<name>springboot-modelmapper</name>
16+
<description>Spring Boot ModelMapper</description>
17+
<!-- <packaging>war</packaging>-->
18+
19+
<properties>
20+
<java.version>1.8</java.version>
21+
<guava.version>27.0.1-jre</guava.version>
22+
<xstream.version>1.4.11.1</xstream.version>
23+
<modelmapper.version>2.3.3</modelmapper.version>
24+
<rest-assured.version>3.0.1</rest-assured.version>
25+
<junit-jupiter.version>5.2.0</junit-jupiter.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.dataformat</groupId>
35+
<artifactId>jackson-dataformat-xml</artifactId>
36+
</dependency>
37+
<!-- We'll need to comment out the jackson-dataformat-xml dependency if we want to use XStream: -->
38+
<dependency>
39+
<groupId>org.springframework</groupId>
40+
<artifactId>spring-oxm</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.thoughtworks.xstream</groupId>
44+
<artifactId>xstream</artifactId>
45+
<version>${xstream.version}</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.projectlombok</groupId>
49+
<artifactId>lombok</artifactId>
50+
<version>1.18.4</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>ch.qos.logback</groupId>
54+
<artifactId>logback-classic</artifactId>
55+
<version>1.2.3</version>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>com.h2database</groupId>
60+
<artifactId>h2</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-data-jpa</artifactId>
65+
</dependency>
66+
67+
<!-- Spring HATEOAS -->
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-hateoas</artifactId>
71+
</dependency>
72+
73+
<!-- util -->
74+
75+
<dependency>
76+
<groupId>com.google.guava</groupId>
77+
<artifactId>guava</artifactId>
78+
<version>${guava.version}</version>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-test</artifactId>
84+
<scope>test</scope>
85+
</dependency>
86+
<dependency>
87+
<groupId>net.sourceforge.htmlunit</groupId>
88+
<artifactId>htmlunit</artifactId>
89+
<scope>test</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>org.modelmapper</groupId>
93+
<artifactId>modelmapper</artifactId>
94+
<version>${modelmapper.version}</version>
95+
</dependency>
96+
<!-- test scoped -->
97+
<dependency>
98+
<groupId>io.rest-assured</groupId>
99+
<artifactId>rest-assured</artifactId>
100+
<version>${rest-assured.version}</version>
101+
<scope>test</scope>
102+
<!-- <exclusions>-->
103+
<!-- <exclusion>-->
104+
<!-- <artifactId>commons-logging</artifactId>-->
105+
<!-- <groupId>commons-logging</groupId>i-->
106+
<!-- </exclusion>-->
107+
<!-- </exclusions>-->
108+
</dependency>
109+
<dependency>
110+
<groupId>org.junit.jupiter</groupId>
111+
<artifactId>junit-jupiter-engine</artifactId>
112+
<version>${junit-jupiter.version}</version>
113+
<scope>test</scope>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.junit.jupiter</groupId>
117+
<artifactId>junit-jupiter-params</artifactId>
118+
<version>${junit-jupiter.version}</version>
119+
<scope>test</scope>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.junit.jupiter</groupId>
123+
<artifactId>junit-jupiter-api</artifactId>
124+
<version>${junit-jupiter.version}</version>
125+
<scope>test</scope>
126+
</dependency>
127+
</dependencies>
128+
129+
<build>
130+
<plugins>
131+
<plugin>
132+
<groupId>org.springframework.boot</groupId>
133+
<artifactId>spring-boot-maven-plugin</artifactId>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung;
2+
3+
import org.modelmapper.ModelMapper;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
8+
@SpringBootApplication
9+
public class SpringBootRestApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringBootRestApplication.class, args);
13+
}
14+
15+
@Bean
16+
public ModelMapper modelMapper() {
17+
return new ModelMapper();
18+
}
19+
20+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.modelmapper.controller;
2+
3+
import java.text.ParseException;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
import org.modelmapper.ModelMapper;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestMethod;
15+
import org.springframework.web.bind.annotation.ResponseBody;
16+
import org.springframework.web.bind.annotation.ResponseStatus;
17+
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;
22+
23+
@Controller
24+
@RequestMapping("/posts")
25+
public class PostRestController {
26+
27+
@Autowired
28+
private IPostService postService;
29+
30+
@Autowired
31+
private IUserService userService;
32+
33+
@Autowired
34+
private ModelMapper modelMapper;
35+
36+
@RequestMapping(method = RequestMethod.GET)
37+
@ResponseBody
38+
public List<PostDto> getPosts(
39+
@PathVariable("page") int page,
40+
@PathVariable("size") int size,
41+
@PathVariable("sortDir") String sortDir,
42+
@PathVariable("sort") String sort) {
43+
44+
List<Post> posts = postService.getPostsList(page, size, sortDir, sort);
45+
return posts.stream()
46+
.map(post -> convertToDto(post))
47+
.collect(Collectors.toList());
48+
}
49+
50+
@RequestMapping(method = RequestMethod.POST)
51+
@ResponseStatus(HttpStatus.CREATED)
52+
@ResponseBody
53+
public PostDto createPost(@RequestBody PostDto postDto) throws ParseException {
54+
Post post = convertToEntity(postDto);
55+
Post postCreated = postService.createPost(post);
56+
return convertToDto(postCreated);
57+
}
58+
59+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
60+
@ResponseBody
61+
public PostDto getPost(@PathVariable("id") Long id) {
62+
return convertToDto(postService.getPostById(id));
63+
}
64+
65+
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
66+
@ResponseStatus(HttpStatus.OK)
67+
public void updatePost(@RequestBody PostDto postDto) throws ParseException {
68+
Post post = convertToEntity(postDto);
69+
postService.updatePost(post);
70+
}
71+
72+
73+
private PostDto convertToDto(Post post) {
74+
PostDto postDto = modelMapper.map(post, PostDto.class);
75+
postDto.setSubmissionDate(post.getSubmissionDate(),
76+
userService.getCurrentUser().getPreference().getTimezone());
77+
return postDto;
78+
}
79+
80+
private Post convertToEntity(PostDto postDto) throws ParseException {
81+
Post post = modelMapper.map(postDto, Post.class);
82+
post.setSubmissionDate(postDto.getSubmissionDateConverted(
83+
userService.getCurrentUser().getPreference().getTimezone()));
84+
85+
if (postDto.getId() != null) {
86+
Post oldPost = postService.getPostById(postDto.getId());
87+
post.setRedditID(oldPost.getRedditID());
88+
post.setSent(oldPost.isSent());
89+
}
90+
return post;
91+
}
92+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.modelmapper.dto;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
import java.util.TimeZone;
7+
8+
public class PostDto {
9+
10+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
11+
12+
private Long id;
13+
14+
private String title;
15+
16+
private String url;
17+
18+
private String date;
19+
20+
private UserDto user;
21+
22+
public Date getSubmissionDateConverted(String timezone) throws ParseException {
23+
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
24+
return dateFormat.parse(this.date);
25+
}
26+
27+
public void setSubmissionDate(Date date, String timezone) {
28+
dateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
29+
this.date = dateFormat.format(date);
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getTitle() {
41+
return title;
42+
}
43+
44+
public void setTitle(String title) {
45+
this.title = title;
46+
}
47+
48+
public String getUrl() {
49+
return url;
50+
}
51+
52+
public void setUrl(String url) {
53+
this.url = url;
54+
}
55+
56+
public String getDate() {
57+
return date;
58+
}
59+
60+
public void setDate(String date) {
61+
this.date = date;
62+
}
63+
64+
public UserDto getUser() {
65+
return user;
66+
}
67+
68+
public void setUser(UserDto user) {
69+
this.user = user;
70+
}
71+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.modelmapper.dto;
2+
3+
public class UserDto {
4+
5+
private String name;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
}

0 commit comments

Comments
 (0)