Orika Mapper Example in Java

In this tutorial, We are going to show you simple example about, Simple Orika Mapper example in Java. The example program has been attached in the same flow.

What is Orika?

Orika is an Object Mapper framework, Its used to covert or clone one object into other object. for more details please visit here

Maven Dependency

<dependency>
<groupId>ma.glasnost.orika</groupId>
<artifactId>orika-core</artifactId>
<version>1.4.6</version>
</dependency>

POJO 1

package com.dineshkrish;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Employee {
private int id;
private String name;
private int age;
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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

POJO 2

package com.dineshkrish;
/**
* 
* @author Dinesh Krishnan
*
*/
public class EmployeeDto {
private int id;
private String name;
private int age;
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;
}
@Override
public String toString() {
return "EmployeeDto [Id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

Mapper Configuration

package com.dineshkrish;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.ConfigurableMapper;
/**
* 
* @author Dinesh Krishnan
*
*/
public class MyMapper extends ConfigurableMapper {
@Override
protected void configure(MapperFactory factory) {
factory
.classMap(Employee.class, EmployeeDto.class)
.byDefault()
.register();
}
}

Example Program

package com.dineshkrish;
import ma.glasnost.orika.MapperFacade;
/**
* 
* @author Dinesh Krishnan
*
*/
public class Example {
public static void main(String[] args) {
MapperFacade mapper = new MyMapper();
Employee e = new Employee();
e.setId(1001);
e.setAge(27);
e.setName("Dinesh Krishnan");
EmployeeDto dto = mapper.map(e, EmployeeDto.class);
System.out.println(dto);
}
}

Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
EmployeeDto [Id=1001, name=Dinesh Krishnan, age=27]

References

1. Orika Documentation
2. Orika Examples

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *