2
2
.
.
1
1
2
2
.
.
2
2
J
J
M
M
a
a
p
p
p
p
e
e
r
r
-
-
U
U
s
s
i
i
n
n
g
g
A
A
n
n
n
n
o
o
t
t
a
a
t
t
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorial shows how to use JMapper's Annotations to convert AuthorBookDTO into Author and Book Entities.
AuthorBookDTO is instantiated from Postman's JSON HTTP Request as described in Annotation - (Spring) @RequestBody.
Alternative to using JMapper is Model Mapper.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller and @RequestMapping. Includes Tomcat Server.
Developer Tools
Lombok
Enables @Data which generate helper methods (setters, getters, ...)
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_dtojmapper (add Spring Boot Starters from the table)
Edit File: pom.xml (manually add JMapper dependency)
Create Package: entities (inside main package)
– Create Class: Author.java (inside package entities)
– Create Class: Book.java (inside package entities)
Create Package: DTOs (inside main package)
– Create Class: AuthorBookDTO.java (inside package controllers)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
pom.xml
<dependency>
<groupId>com.googlecode.jmapper-framework</groupId>
<artifactId>jmapper-core</artifactId>
<version>1.6.0.1</version>
</dependency>
MyController
AuthorBookDTO
POSTMAN
http://localhost:8080/AddAuthorBook
Author
Book
HTTP POST Request
Author.java
package com.ivoronline.springboot_dto_jmapper.entities;
import com.googlecode.jmapper.annotations.JMap;
import lombok.Data;
@Data
public class Author {
public Integer id; //Can't be mapped since there is no Annotation
@JMap("authorName") //Name of the Source Property
public String name;
@JMap("authorAge") //Name of the Source Property
public Integer age;
}
Book.java
package com.ivoronline.springboot_dto_jmapper.entities;
import com.googlecode.jmapper.annotations.JMap;
import lombok.Data;
@Data
public class Book {
public Integer id;
@JMap //Source Property should have the same name
public String title;
}
AuthorBookDTO.java
package com.ivoronline.springboot_dto_jmapper.DTO;
import lombok.Data;
@Data
public class AuthorBookDTO {
public String authorName;
public Integer authorAge;
public String title;
}
MyController.java
package com.ivoronline.springboot_dto_jmapper.controllers;
import com.googlecode.jmapper.JMapper;
import com.ivoronline.springboot_dto_jmapper.DTO.AuthorBookDTO;
import com.ivoronline.springboot_dto_jmapper.entities.Author;
import com.ivoronline.springboot_dto_jmapper.entities.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@ResponseBody
@RequestMapping("AddAuthorBook")
public String addAuthorBook(@RequestBody AuthorBookDTO authorBookDTO) {
//INSTANTIATE JMAPPERS<DESTINATION, SOURCE>
JMapper<Book, AuthorBookDTO> bookMapper = new JMapper<>(Book .class, AuthorBookDTO.class);
JMapper<Author, AuthorBookDTO> authorMapper = new JMapper<>(Author.class, AuthorBookDTO.class);
//MATP AuthorBookDTO TO AUTHOR & BOOK.
Book book = bookMapper .getDestination(authorBookDTO);
Author author = authorMapper.getDestination(authorBookDTO);
//RETURN SOMETHING
return author.name + " has written: " + book.title;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
Start Postman
POST: http://localhost:8080/AddAuthorBook
Headers: (copy from below)
Body: (copy from below)
Send
Headers (add Key-Value)
Content-Type: application/json
Body (option: raw)
{
"name" : "John",
"age" : 20,
"title" : "Book about dogs"
}
HTTP Response Body
John has written Book about dogs
HTTP Request Headers (Bulk Edit) Request JSON Body (raw) & Response Body
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.googlecode.jmapper-framework</groupId>
<artifactId>jmapper-core</artifactId>
<version>1.6.0.1</version>
</dependency>
</dependencies>