Tuesday 3 August 2021

Modelmapper: specify custom mappings

Using 'addMappings' method of ModelMapper class, you can explicitly specify the mappings.

 

Example

ModelMapper modelMapper = new ModelMapper();

modelMapper.addMappings(new PropertyMap<User, UserDto>() {
	@Override
	protected void configure() {

		map().getAddress().setCity(source.getCity());
		map().getAddress().setCountry(source.getCity());
		map().getAddress().setStreet(source.getStreet());

		map().setUserAge(source.getAge());
	}
});

 

Find the below working application.

 

Address.java

package com.sample.app.model;

public class Address {

	private String street;
	private String city;
	private String country;

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return "Address [street=" + street + ", city=" + city + ", country=" + country + "]";
	}

}

 

User.java

package com.sample.app.model;

public class User {

	private int id;
	private String name;
	private int age;

	private String street;
	private String city;
	private String country;

	public User() {

	}

	public User(int id, String name, int age, String street, String city, String country) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.street = street;
		this.city = city;
		this.country = country;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", age=" + age + ", street=" + street + ", city=" + city
				+ ", country=" + country + "]";
	}

}

 

UserDto.java

package com.sample.app.model;

public class UserDto {
	private int id;
	private String name;
	private int userAge;

	private Address address;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getUserAge() {
		return userAge;
	}

	public void setUserAge(int userAge) {
		this.userAge = userAge;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "UserDto [id=" + id + ", name=" + name + ", userAge=" + userAge + ", address=" + address + "]";
	}

}

ExplicitMappingDemo.java

package com.sample.app;

import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;

import com.sample.app.model.User;
import com.sample.app.model.UserDto;

public class ExplicitMappingDemo {

	public static void main(String args[]) {
		ModelMapper modelMapper = new ModelMapper();

		modelMapper.addMappings(new PropertyMap<User, UserDto>() {
			@Override
			protected void configure() {

				map().getAddress().setCity(source.getCity());
				map().getAddress().setCountry(source.getCity());
				map().getAddress().setStreet(source.getStreet());

				map().setUserAge(source.getAge());
			}
		});

		User user = new User(1, "Krishna", 31, "Chowdeswari street", "Bangalore", "India");

		UserDto userDto = modelMapper.map(user, UserDto.class);

		System.out.println(userDto);

	}

}


Output

UserDto [id=1, name=Krishna, userAge=31, address=Address [street=Chowdeswari street, city=Bangalore, country=Bangalore]]

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment