본문 바로가기
Back-end/Spring Boot + REST API

Spring boot - blog application (REST API) : ModelMapper

by javapp 자바앱 2022. 7. 4.
728x90

 

ModelMapper map an object from one object to another object !

 

 

1. Add ModelMapper Library dependency

 

 

Maven Repository: org.modelmapper » modelmapper

 

mvnrepository.com

<!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>3.1.0</version>
</dependency>

 

 

2. define the ModelMapper bean in our Spring configuration

 

@SpringBootApplication indicates that configuration class that declares

one or more bean methods and also triggers a auto configuration and component scanning

 

configure a bean for modelMapper class

@SpringBootApplication
public class SpringbootBlogRestApiApplication {
	
	@Bean
	public ModelMapper modelMapper() {
		return new ModelMapper();
	}

	public static void main(String[] args) {
		SpringApplication.run(SpringbootBlogRestApiApplication.class, args);
	}
}

 

 

 3. Inject and use ModelMapper

@Service
public class PostServiceImpl implements PostService
{
	private PostRepository postRepository;
	
	private ModelMapper mapper;
	
	public PostServiceImpl(PostRepository postRepository, ModelMapper mapper) {
		super();
		this.postRepository = postRepository;
		this.mapper = mapper;
	}
    
    ...
}

 

 

apply modelMapper

	// convert DTO to entity
	private Post mapToEntity(PostDto postDto)
	{
		Post post = mapper.map(postDto, Post.class);
		
//		Post post = new Post();
//		post.setTitle(postDto.getTitle());
//		post.setDescription(postDto.getDescription());
//		post.setContent(postDto.getContent());
		return post;
	}
	
	// convert entity to DTO
	private PostDto mapToDTO(Post post)
	{
		PostDto postdto =  mapper.map(post, PostDto.class);
		
//		PostDto postdto = new PostDto();
//		postdto.setId(post.getId());
//		postdto.setTitle(post.getTitle());
//		postdto.setDescription(post.getDescription());
//		postdto.setContent(post.getContent());
		return postdto;
	}

 

We can do reduce it by 4 to 5 lines using modelMapper !

 

Also apply the same to Comment !

	private CommentDto mapToDTO(Comment comment)
	{
		CommentDto commentDto = modelMapper.map(comment, CommentDto.class);
//		CommentDto commentDto = new CommentDto();
//		commentDto.setId(comment.getId());
//		commentDto.setName(comment.getName());
//		commentDto.setEmail(comment.getEmail());
//		commentDto.setBody(comment.getBody());
		return commentDto;
	}
	private Comment mapToEntity(CommentDto commentDto)
	{
		Comment comment = modelMapper.map(commentDto, Comment.class);

//		Comment comment = new Comment();
//		comment.setId(commentDto.getId());
//		comment.setName(commentDto.getName());
//		comment.setEmail(commentDto.getEmail());
//		comment.setBody(commentDto.getBody());
		return comment;
	}

 


 

현재 포스트를 조회하면 댓글이 조회가 안된다. 그래서

 

포스트 하나를 조회하면 댓글 또한 조회가 되게 하려고 합니다.

 

@Data
public class PostDto 
{
	private long id;
	private String title;
	private String description;
	private String content;
	
	private Set<CommentDto> comments;
}

private Set<CommentDto> comments; 

추가해서 댓글들을 담으려는데 ,modelMapper로 인해 무한 루프가 발생 

Post class에서 toString() 제거

 

 

Post

//@Data // getter setter requiredArgsConstructor toString
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor

@Entity
@Table(name="posts", uniqueConstraints = {@UniqueConstraint(columnNames= {"title"})})
public class Post 
{

 

TEST

 

 

※ 엔티티에서는 @Data 를 자제하자! --> @Getter @Setter

 

 

댓글