Skip to content

Commit 24dba21

Browse files
committed
added dozer tutorial maven project
1 parent 12e8296 commit 24dba21

16 files changed

Lines changed: 748 additions & 0 deletions

File tree

‎dozer-tutorial/pom.xml‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>dozer-tutorial</artifactId>
6+
<version>1.0</version>
7+
<name>Dozer</name>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>org.apache.maven.plugins</groupId>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<version>3.3</version>
14+
<configuration>
15+
<source>7</source>
16+
<target>7</target>
17+
</configuration>
18+
</plugin>
19+
</plugins>
20+
</build>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.slf4j</groupId>
24+
<artifactId>slf4j-api</artifactId>
25+
<version>1.7.5</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>jcl-over-slf4j</artifactId>
31+
<version>1.7.5</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.apache.commons</groupId>
36+
<artifactId>commons-lang3</artifactId>
37+
<version>3.2.1</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>commons-beanutils</groupId>
42+
<artifactId>commons-beanutils</artifactId>
43+
<version>1.9.1</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>net.sf.dozer</groupId>
48+
<artifactId>dozer</artifactId>
49+
<version>5.5.1</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>junit</groupId>
53+
<artifactId>junit</artifactId>
54+
<version>4.3</version>
55+
<scope>test</scope>
56+
</dependency>
57+
58+
</dependencies>
59+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.dozer;
2+
3+
public class Dest {
4+
private String name;
5+
private int age;
6+
7+
public Dest() {
8+
9+
}
10+
11+
public Dest(String name, int age) {
12+
super();
13+
this.name = name;
14+
this.age = age;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public int getAge() {
26+
return age;
27+
}
28+
29+
public void setAge(int age) {
30+
this.age = age;
31+
}
32+
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.dozer;
2+
3+
public class Dest2 {
4+
private int id;
5+
private int points;
6+
7+
public Dest2() {
8+
9+
}
10+
11+
public Dest2(int id, int points) {
12+
super();
13+
this.id = id;
14+
this.points = points;
15+
}
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public int getPoints() {
26+
return points;
27+
}
28+
29+
public void setPoints(int points) {
30+
this.points = points;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Dest2 [id=" + id + ", points=" + points + "]";
36+
}
37+
38+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.dozer;
2+
3+
import java.text.DateFormat;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
8+
import org.dozer.CustomConverter;
9+
import org.dozer.MappingException;
10+
11+
public class MyCustomConvertor implements CustomConverter {
12+
13+
@Override
14+
public Object convert(Object dest, Object source, Class<?> arg2,
15+
Class<?> arg3) {
16+
if (source == null) {
17+
return null;
18+
}
19+
if (source instanceof Personne3) {
20+
Personne3 person = (Personne3) source;
21+
Date date = new Date(person.getDtob());
22+
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
23+
String isoDate = format.format(date);
24+
return new Person3(person.getName(), isoDate);
25+
26+
} else if (source instanceof Person3) {
27+
Person3 person = (Person3) source;
28+
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
29+
Date date = null;
30+
try {
31+
date = format.parse(person.getDtob());
32+
33+
} catch (ParseException e) {
34+
throw new MappingException("Converter MyCustomConvertor "
35+
+ "used incorrectly:" + e.getMessage());
36+
}
37+
long timestamp = date.getTime();
38+
return new Personne3(person.getName(), timestamp);
39+
40+
} else {
41+
throw new MappingException("Converter MyCustomConvertor "
42+
+ "used incorrectly. Arguments passed in were:" + dest
43+
+ " and " + source);
44+
45+
}
46+
}
47+
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.dozer;
2+
3+
public class Person {
4+
private String name;
5+
private String nickname;
6+
private int age;
7+
8+
public Person() {
9+
10+
}
11+
12+
public Person(String name, String nickname, int age) {
13+
super();
14+
this.name = name;
15+
this.nickname = nickname;
16+
this.age = age;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getNickname() {
28+
return nickname;
29+
}
30+
31+
public void setNickname(String nickname) {
32+
this.nickname = nickname;
33+
}
34+
35+
public int getAge() {
36+
return age;
37+
}
38+
39+
public void setAge(int age) {
40+
this.age = age;
41+
}
42+
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.dozer;
2+
3+
public class Person2 {
4+
private String name;
5+
private String nickname;
6+
private int age;
7+
8+
public Person2() {
9+
10+
}
11+
12+
public Person2(String name, String nickname, int age) {
13+
super();
14+
this.name = name;
15+
this.nickname = nickname;
16+
this.age = age;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getNickname() {
28+
return nickname;
29+
}
30+
31+
public void setNickname(String nickname) {
32+
this.nickname = nickname;
33+
}
34+
35+
public int getAge() {
36+
return age;
37+
}
38+
39+
public void setAge(int age) {
40+
this.age = age;
41+
}
42+
43+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.dozer;
2+
3+
public class Person3 {
4+
private String name;
5+
private String dtob;
6+
7+
public Person3() {
8+
9+
}
10+
11+
public Person3(String name, String dtob) {
12+
super();
13+
this.name = name;
14+
this.dtob = dtob;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getDtob() {
26+
return dtob;
27+
}
28+
29+
public void setDtob(String dtob) {
30+
this.dtob = dtob;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Person3 [name=" + name + ", dtob=" + dtob + "]";
36+
}
37+
38+
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.dozer;
2+
3+
public class Personne {
4+
private String nom;
5+
private String surnom;
6+
private int age;
7+
8+
public Personne() {
9+
10+
}
11+
12+
public Personne(String nom, String surnom, int age) {
13+
super();
14+
this.nom = nom;
15+
this.surnom = surnom;
16+
this.age = age;
17+
}
18+
19+
public String getNom() {
20+
return nom;
21+
}
22+
23+
public void setNom(String nom) {
24+
this.nom = nom;
25+
}
26+
27+
public String getSurnom() {
28+
return surnom;
29+
}
30+
31+
public void setSurnom(String surnom) {
32+
this.surnom = surnom;
33+
}
34+
35+
public int getAge() {
36+
return age;
37+
}
38+
39+
public void setAge(int age) {
40+
this.age = age;
41+
}
42+
43+
}

0 commit comments

Comments
 (0)