Entity to DTO using Model Mapper in Spring Restful Web services

Entity to DTO using Model Mapper in Spring Restful Web services:

In spring restful webservices we can not directly expose our entity pojo classes as json responses as it has lot of issues like security/unwanted data’s exposure etc.

Data Transfer Object (DTO) is a design pattern which actually resolves this issue. I am going to do these sample in spring boot project.

In highlevel idea behind this is,

  1. Create the DTO pojo classes for all/required of your Entities.
    2. Write a model mapper for pojo to entity and vice versa.
    3. In controller do the converstions before starting any business.

Entity to DTO using Model Mapper in Spring Restful Web services


If you have a entity class called coupon with lot of fields & mappings like store/category/tag inside coupon entity class and you would want to just expose only the few fields of your coupon entity then create your DTO class named couponDTO and keep only the required fields inside it.

Say Coupon Entity has

coupon_id, coupon_name, coupon_desc, coupon_url, isActive, uploadedBy, uploadedDt, updatedBy, updatedDt

And you just want to expose only the

coupn_name, coupon_desc and coupon_url

then create the DTO class named CouponDTO and keep only these three fields and its getters setters.


Step 1: Put the below entry in your pom.xml




Step 2: Add the following @Bean in Spring Boot’s Main Application class.

This is to load the model mapper in spring, so that the same can be autowired whereever required.

public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
public ModelMapper modelMapper() {
return new ModelMapper();



Step 3: My Coupon Entity and CouponDTO pojo classes are:

  • Here Model Mapper maps with the correct fields in most of the cases by matching the field hashcode, so even if the field names are different I was able to see that it mapped to the correct field only.
  • If you face any issues then you can keep the same names in both the classes (entity & DTO).


Coupon Entity:

@Table(name = "cpn")
public class Coupon implements Serializable {
public Coupon() {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long couponId;

@Column(name = "title")
private String couponTitle;

@Column(name = "tc_desc")
private String couponDesc;

@Column(name = "is_cpn_active")
private char isCouponActive;

// and many other fields with getters / setters for all the fields.




public class CouponDTO {
private Long id;

private String title;

private String desc;



Step 4: Converting Entity to DTO and Vice Versa:

  • Autowire the ModelMapper
  • Write DTO to Entity and entity to DTO methods.
  • Here I used Stream to iterate, convert and collect it in the list, which can be normally iterated and performed using loops too.
  • I explained everything by keeping in controller layer only, use it in service layer(recommended) or anywhere you want.
public class ProductController {

private ModelMapper modelMapper;

public List<CouponDTO> getCoupons(){
// here my business logic to get the List<Coupon> and put it in couponList | Coupon is my entity class.
// Converting coupon entity to couponDTO entity
List<CouponDTO> couponDTOList = couponList.stream().map(eachCoupon -> convertToDto(eachCoupon))
return couponDTOList;

private CouponDTO convertToDto(Coupon coupon) {
CouponDTO couponDTO = modelMapper.map(coupon, CouponDTO.class);
return couponDTO;

private Coupon convertToEntity(CouponDTO couponDTO) throws ParseException {
Coupon coupon = modelMapper.map(couponDTO, Coupon.class);
return coupon;


Now by returning the CouponDTO instead coupon entity you can have the control over the fields which gets exposed in the web service.



  • Further documentation and other stuffs related to modelmapper can be found here, And in Github.
  • MapStruct is the another good alternative for the same requirement.
  • We can also make use @JsonIgnore and other similar annotations to expose only the required fields from our entity beans. But the problem is we can’t have many different versions of the same beans.
  • Eg: If suppose Downstream system A want 3 fields of Coupon and Downstream system B wants 5 fields of Coupon then if we make the changes in Coupon Entity directly with @JsonIgnore or any other Jackson provided annotations then it affects across the entity and the different version can’t ever be created.
  • It is also difficult to add the bean validation annotations to bean classes, which will also be confusing if hibernate entity annotations and validation annotations remains in the same class. So dividing the entity and DTO can also make the validation process simplified, so that only the validation related annotations can be kept inside the DTO classes.


Feel free to post your comments / suggestions / tips in the below comments section.

Hope it helps!

669 total views, 2 views today

About Mirthbees 679 Articles
Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in

Be the first to comment

Leave a Reply

Your email address will not be published.


This site uses Akismet to reduce spam. Learn how your comment data is processed.