Introduction
This example using the application written in this article ( Click Here ). Additionally we have added the following dependence into pom.xml file<dependency> <groupId>org.modelmapper</groupId> <artifactId>modelmapper</artifactId> <version>2.3.5</version> </dependency>
What is ModelMapper?
ModelMapper is a java based DTO to Entity and vice versa converting library.
For full details on ModelMapper goto ModelMapper website ( Click Here )
Entity to DTO |
DTO is acting as a data transfer object between applications. From the above figure you can notice that gender as number (1) in Entity class where as DTO shows gender as a String value ( Male ).
0 represent Female and 1 represents Male in database table.
Creating DTO class
package com.jai43.jsite.dto; public class PersonDTO { int personID; String pnameString; String gender; public int getPersonID() { return personID; } public void setPersonID(int personID) { this.personID = personID; } public String getPnameString() { return pnameString; } public void setPnameString(String pnameString) { this.pnameString = pnameString; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((gender == null) ? 0 : gender.hashCode()); result = prime * result + personID; result = prime * result + ((pnameString == null) ? 0 : pnameString.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; PersonDTO other = (PersonDTO) obj; if (gender == null) { if (other.gender != null) return false; } else if (!gender.equals(other.gender)) return false; if (personID != other.personID) return false; if (pnameString == null) { if (other.pnameString != null) return false; } else if (!pnameString.equals(other.pnameString)) return false; return true; } }
You can observe that we are returning person id as integer , person name in capital letters as a string and Gender string instead of number.
Using PropertyMap for mapping between source( entity ) and destination ( DTO )
We can PropertyMap to map variable externally, by default same named and data type variables between source and destination can be mapped by MapModel. But in our case ( i mean in this example ) source variable is different than destination variable, for example pid is the variable representing person id in Person Entity , but personID is the variable representing person id in PersonDTO as shown in the above figure.Gender is integer number in entity class, where it is String in DTO class. To do these mappings we need to do with external property mappings process only. To do this we need to create a mapper class which extends PropertyMap
package com.jai43.jsite.modelMappers; import org.modelmapper.Converter; import org.modelmapper.PropertyMap; import org.springframework.stereotype.Component; import com.jai43.jsite.dto.PersonDTO; import com.jai43.jsite.entities.Person; @Component public class PersonMapper extends PropertyMap<Person, PersonDTO> { private Converter<String, String> toUpperCase = (n) -> { return n.getSource().toUpperCase(); }; private Converter<Integer, String> genger = (g) -> { int gcode = g.getSource().intValue(); String gString = (gcode > 0) ? "Male" : "Female"; return gString; }; @Override protected void configure() { // TODO Auto-generated method stub using(toUpperCase).map(source.getPname(), destination.getPnameString()); using(genger).map(source.getPgender(), destination.getGender()); map(source.getPid(), destination.getPersonID()); } }
In the above example we have created two converts , to convert String into Uppercase and which is using in configure() method, and we have create another converter named as gender to return String value by based on gender integer value of Entity variable ( source variable ), finally with map(source.field, destination.field) method we have done mapping between pid and personID. All these done with external mapping with the help of PropertyMap class.
We have to add this PersonMapper to ModelMapper object before mapping source with destination.
Adding PropertyMap with ModelMapper object and mapping source with destination
As a first step we have to create ModelMapper object, then we have to add our PersonMapper which is a PropertyMap class, then we have to use map() method by passing source and destination objects , where source is Entity object and destination is DTO object...
We have done all above steps in PersonService.java class , we can use this in Controller REST endpoint.
public PersonDTO getPersonDTO(long pid) throws Exception { Person sourcePerson = this.getPersonById(pid); ModelMapper modelMapper = new ModelMapper(); modelMapper.addMappings(personMapper); PersonDTO destinationPersonDTO = modelMapper.map(sourcePerson, PersonDTO.class); return destinationPersonDTO; }
The above method added to PersonService.java class which we done in our previous example.
REST endpoints in Controller
Here we have created a new Controller class in our previous example code.
package com.jai43.controller.external; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.jai43.jsite.dto.PersonDTO; import com.jai43.jsite.services.PersonService; @RestController @RequestMapping("/per") public class PersonControllerDTO { @Autowired PersonServiceper; @GetMapping("/p/{id}") public PersonDTO getPersonDTOByID(@PathVariable("id") long pid) throws Exception { return per.getPersonDTO(pid); } }
Output Screens
Here we have used ARC as a REST client to test application you can get it from download ARC
Post a Comment
Post a Comment