Step 1: Add given dependency in pom.xml file.
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
</dependency>
Step 2: Go to your main file and copy & paste given code.
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
@Bean
public ModelMapper modelMapperBean() {
return new ModelMapper();
}
Step 3: Go to your file where you want to map one object data to another object.
@Autowired
private ModelMapper modelMapper;
Step 4: For single object mapping
UserDto userDTO = modelMapper.map(user, UserDTO.class);
Step 5: For list Object mapping
List<UserDto> userDtoList= modelMapper.map(userList, new TypeToken<List<UserDto>>() {}.getType());
NOTE:-
- UserDto is the blank object.
- user is the object where data is available.
- So here we are mapping user data to UserDto.