File tree Expand file tree Collapse file tree 4 files changed +147
-0
lines changed
mapstruct-mapping-from-map/src
main/java/org/mapstruct/example
test/java/org/mapstruct/example Expand file tree Collapse file tree 4 files changed +147
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright MapStruct Authors.
3+ *
4+ * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+ */
6+ package org .mapstruct .example .dto ;
7+
8+ /**
9+ * @author Filip Hrisafov
10+ */
11+ public class Department {
12+
13+ private String id ;
14+ private String name ;
15+
16+ public String getId () {
17+ return id ;
18+ }
19+
20+ public void setId (String id ) {
21+ this .id = id ;
22+ }
23+
24+ public String getName () {
25+ return name ;
26+ }
27+
28+ public void setName (String name ) {
29+ this .name = name ;
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright MapStruct Authors.
3+ *
4+ * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+ */
6+ package org .mapstruct .example .dto ;
7+
8+ /**
9+ * @author Filip Hrisafov
10+ */
11+ public class Employee {
12+
13+ private String id ;
14+ private String name ;
15+ private Department department ;
16+
17+ public String getId () {
18+ return id ;
19+ }
20+
21+ public void setId (String id ) {
22+ this .id = id ;
23+ }
24+
25+ public String getName () {
26+ return name ;
27+ }
28+
29+ public void setName (String name ) {
30+ this .name = name ;
31+ }
32+
33+ public Department getDepartment () {
34+ return department ;
35+ }
36+
37+ public void setDepartment (Department department ) {
38+ this .department = department ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright MapStruct Authors.
3+ *
4+ * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+ */
6+ package org .mapstruct .example .mapper ;
7+
8+ import java .lang .annotation .Target ;
9+ import java .util .Map ;
10+
11+ import org .mapstruct .AfterMapping ;
12+ import org .mapstruct .Mapper ;
13+ import org .mapstruct .Mapping ;
14+ import org .mapstruct .MappingTarget ;
15+ import org .mapstruct .example .dto .Department ;
16+ import org .mapstruct .example .dto .Employee ;
17+ import org .mapstruct .factory .Mappers ;
18+
19+ /**
20+ * @author Filip Hrisafov
21+ */
22+ @ Mapper
23+ public interface MapToBeanMapper {
24+
25+ MapToBeanMapper INSTANCE = Mappers .getMapper (MapToBeanMapper .class );
26+
27+ @ Mapping (target = "department" , ignore = true )
28+ Employee fromMap (Map <String , String > map );
29+
30+ @ AfterMapping
31+ default void finishEmployee (@ MappingTarget Employee employee , Map <String , String > map ) {
32+ employee .setDepartment (fromMapToDepartment (map ));
33+ }
34+
35+ @ Mapping (target = "id" , source = "did" )
36+ @ Mapping (target = "name" , source = "dname" )
37+ Department fromMapToDepartment (Map <String , String > map );
38+ }
Original file line number Diff line number Diff line change 1+ package org .mapstruct .example ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotNull ;
5+
6+ import java .util .HashMap ;
7+ import java .util .Map ;
8+
9+ import org .junit .Test ;
10+ import org .mapstruct .example .dto .Department ;
11+ import org .mapstruct .example .dto .Employee ;
12+ import org .mapstruct .example .mapper .MapToBeanMapper ;
13+
14+ /**
15+ * @author Filip Hrisafov
16+ */
17+ public class MapToBeanMapperTest {
18+
19+ @ Test
20+ public void shouldMapMapToBean () {
21+ Map <String , String > map = new HashMap <>();
22+ map .put ("id" , "1234" );
23+ map .put ("name" , "Tester" );
24+ map .put ("did" , "4321" ); //Department Id
25+ map .put ("dname" , "Test" );// Depart name
26+
27+ Employee employee = MapToBeanMapper .INSTANCE .fromMap (map );
28+
29+ assertNotNull (employee );
30+ assertEquals (employee .getId (), "1234" );
31+ assertEquals (employee .getName (), "Tester" );
32+
33+ Department department = employee .getDepartment ();
34+ assertNotNull (department );
35+ assertEquals (department .getId (), "4321" );
36+ assertEquals (department .getName (), "Test" );
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments