This tutorial extends Orika Bean Mapping Tutorial by using Spring to configure Orika.
Step 1: Create a simple Maven project from a command-line. Just press enter for all the prompts.
1 | mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-orika |
Step 2: Import the above “simple-orika” folder in the file system into eclipse. File –> Import –> “Existing Maven Projects” and then select the “simple-orika” folder that was created by the “Step 1” from the file system.
Step 3: Open the pom.xml file and add the dependencies for Orika.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mytutorial</groupId> <artifactId>simple-orika</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>simple-orika</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.13.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Orika mapping framework --> <dependency> <groupId>ma.glasnost.orika</groupId> <artifactId>orika-core</artifactId> <version>1.4.0</version> </dependency> <!-- logging framework --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> <!-- Spring Dependencies (core libraries) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project> |
Step 4: Define the source and target beans.
Source Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.mytutorial; import java.io.Serializable; public class PersonDomain implements Serializable{ private static final long serialVersionUID = 1L; private String name; private int age; 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; } @Override public String toString() { return "PersonDomain [name=" + name + ", age=" + age + "]"; } } |
Target Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package com.mytutorial; import java.io.Serializable; public class PersonDto implements Serializable { private static final long serialVersionUID = 1L; private String nameFull; private int ageThisYear; public String getNameFull() { return nameFull; } public void setNameFull(String nameFull) { this.nameFull = nameFull; } public int getAgeThisYear() { return ageThisYear; } public void setAgeThisYear(int ageThisYear) { this.ageThisYear = ageThisYear; } @Override public String toString() { return "PersonDto [nameFull=" + nameFull + ", ageThisYear=" + ageThisYear + "]"; } } |
Step 5: Orika custom mapper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.mytutorial; import ma.glasnost.orika.MapperFactory; import ma.glasnost.orika.impl.ConfigurableMapper; import org.springframework.stereotype.Component; @Component public class MyCustomMapper extends ConfigurableMapper { protected void configure(MapperFactory factory) { factory.classMap(PersonDomain.class, PersonDto.class) .field("name", "nameFull") .field("age", "ageThisYear").byDefault() .register(); } } |
Step 6: Spring DI Java config.
1 2 3 4 5 6 7 8 9 10 | package com.mytutorial; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages={"com.mytutorial"}) public class AppConfig { } |
Step 7: Stand alone class with Spring for wiring up vi DI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.mytutorial; import ma.glasnost.orika.MapperFacade; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Component; @Component public class SimpleTest { @Autowired MapperFacade mapperFacade; public static void main(String[] args) throws Exception { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); SimpleTest st = context.getBean(SimpleTest.class); st.mapPesonDomainToPersonDto(); } private void mapPesonDomainToPersonDto() { PersonDomain p1Domain = new PersonDomain(); p1Domain.setName("John Smith"); p1Domain.setAge(25); //map source: p1Domain to target:p1Dto using orika PersonDto p1Dto = mapperFacade.map(p1Domain, PersonDto.class); System.out.println("after mapping with orika: p1Dto = " + p1Dto); } } |
Output:
1 | after mapping with orika: p1Dto = PersonDto [nameFull=John Smith, ageThisYear=25] |