Skip to content

Commit d6d1d28

Browse files
committed
Convert over to use MapStruct
1 parent d7deedd commit d6d1d28

15 files changed

Lines changed: 245 additions & 65 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Mars Sim REST Pilot
22

3-
This project is a simple SpringBoot RestAPI server to prototype an API for the mars-sim project to support a REactJS client.
3+
This project is a simple SpringBoot RestAPI server to prototype an API for the mars-sim project to support a ReactJS client.
44

55
## Projects
66
There are 2 sub-projects:
77

88
### Rest-server
9-
A Spring boot Java app that provides a RestAPI of entities. It uses the [ModelMapper](http://modelmapper.org/) to automatically provide the mapping code from the model objects to the various DTO equivalents. There is a _model_ package that emulates the model objects of the Simulation.
9+
A Spring boot Java app that provides a RestAPI of entities. It uses the [MapStruct](https://mapstruct.org) to automatically generate the mapping code from the model objects to the various DTO equivalents. There is a _model_ package that emulates the model objects of the Simulation. The package _dto_ contains the DTO exposed to the client. The _mapper_ package contains the Mappers that define any custom mappings needed for the DTOs. MapStruct has a default mapping logic but it can be overridden.
1010

1111
The Rest API is exposed under the _/api_ endpoint and follows the standard approach of entity & idenfitier as URL paths. Filters applied as request parameters.
1212

rest-server/pom.xml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
<properties>
1515
<java.version>11</java.version>
1616
<maven.compiler.release>11</maven.compiler.release>
17-
<modelmapper.version>3.0.0</modelmapper.version>
1817
<openapi.version>1.6.4</openapi.version>
1918
<springboot.version>2.6.3</springboot.version>
19+
<lombok.version>1.18.16</lombok.version>
20+
<mapstruct.version>1.5.2.Final</mapstruct.version>
21+
<lombok-binding.version>0.2.0</lombok-binding.version>
2022
<mainClass>org.mars_sim.rest.App</mainClass>
2123
</properties>
2224
<dependencyManagement>
@@ -50,9 +52,9 @@
5052
<artifactId>spring-boot-starter-actuator</artifactId>
5153
</dependency>
5254
<dependency>
53-
<groupId>org.modelmapper</groupId>
54-
<artifactId>modelmapper</artifactId>
55-
<version>${modelmapper.version}</version>
55+
<groupId>org.mapstruct</groupId>
56+
<artifactId>mapstruct</artifactId>
57+
<version>${mapstruct.version}</version>
5658
</dependency>
5759
<dependency>
5860
<groupId>org.springdoc</groupId>
@@ -62,7 +64,8 @@
6264
<dependency>
6365
<groupId>org.projectlombok</groupId>
6466
<artifactId>lombok</artifactId>
65-
<optional>true</optional>
67+
<version>${lombok.version}</version>
68+
<scope>provided</scope>
6669
</dependency>
6770
<dependency>
6871
<groupId>org.springframework.boot</groupId>
@@ -79,6 +82,23 @@
7982
<version>3.8.0</version>
8083
<configuration>
8184
<release>11</release>
85+
<annotationProcessorPaths>
86+
<path>
87+
<groupId>org.mapstruct</groupId>
88+
<artifactId>mapstruct-processor</artifactId>
89+
<version>${mapstruct.version}</version>
90+
</path>
91+
<path>
92+
<groupId>org.projectlombok</groupId>
93+
<artifactId>lombok</artifactId>
94+
<version>${lombok.version}</version>
95+
</path>
96+
<path>
97+
<groupId>org.projectlombok</groupId>
98+
<artifactId>lombok-mapstruct-binding</artifactId>
99+
<version>${lombok-binding.version}</version>
100+
</path>
101+
</annotationProcessorPaths>
82102
</configuration>
83103
</plugin>
84104
<plugin>

rest-server/src/main/java/org/mars_sim/rest/App.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.mars_sim.rest;
22

33
import org.mars_sim.rest.model.Simulation;
4-
import org.modelmapper.ModelMapper;
54
import org.springframework.boot.ApplicationArguments;
65
import org.springframework.boot.SpringApplication;
76
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -18,10 +17,4 @@ public Simulation getSimulation(ApplicationArguments args) {
1817
System.out.println("Commandline args = " + args);
1918
return new Simulation();
2019
}
21-
22-
@Bean
23-
public ModelMapper getModelMapper() {
24-
ModelMapper mapper = new ModelMapper();
25-
return mapper;
26-
}
2720
}

rest-server/src/main/java/org/mars_sim/rest/controller/PersonController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import java.util.stream.Collectors;
55
import java.util.stream.Stream;
66

7+
import org.mapstruct.factory.Mappers;
78
import org.mars_sim.rest.dto.PersonDTO;
89
import org.mars_sim.rest.dto.PersonSummaryDTO;
10+
import org.mars_sim.rest.mapper.PersonMapper;
911
import org.mars_sim.rest.model.Person;
1012
import org.mars_sim.rest.model.Simulation;
11-
import org.modelmapper.ModelMapper;
1213
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.web.bind.annotation.GetMapping;
1415
import org.springframework.web.bind.annotation.PathVariable;
@@ -23,8 +24,7 @@ public class PersonController {
2324
@Autowired
2425
private Simulation sim;
2526

26-
@Autowired
27-
private ModelMapper mapper;
27+
private PersonMapper mapper = Mappers.getMapper(PersonMapper.class);
2828

2929
@GetMapping("")
3030
List<PersonSummaryDTO> getPersons(@RequestParam(required=false) Integer settlementId) {
@@ -35,12 +35,12 @@ List<PersonSummaryDTO> getPersons(@RequestParam(required=false) Integer settleme
3535
else {
3636
found = sim.getPersons().stream();
3737
}
38-
return found.map(s -> mapper.map(s, PersonSummaryDTO.class)).collect(Collectors.toList());
38+
return found.map(s -> mapper.toSummary(s)).collect(Collectors.toList());
3939
}
4040

4141
@GetMapping("/{id}")
4242
public PersonDTO getPerson(@PathVariable int id) {
4343
Person s = sim.getPersons().get(id-1);
44-
return mapper.map(s, PersonDTO.class);
44+
return mapper.toFull(s);
4545
}
4646
}

rest-server/src/main/java/org/mars_sim/rest/controller/SettlementController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import java.util.List;
44
import java.util.stream.Collectors;
55

6+
import org.mapstruct.factory.Mappers;
67
import org.mars_sim.rest.dto.SettlementDTO;
78
import org.mars_sim.rest.dto.SettlementSummaryDTO;
9+
import org.mars_sim.rest.mapper.SettlementMapper;
810
import org.mars_sim.rest.model.Settlement;
911
import org.mars_sim.rest.model.Simulation;
10-
import org.modelmapper.ModelMapper;
1112
import org.springframework.beans.factory.annotation.Autowired;
1213
import org.springframework.web.bind.annotation.GetMapping;
1314
import org.springframework.web.bind.annotation.PathVariable;
@@ -21,17 +22,16 @@ public class SettlementController {
2122
@Autowired
2223
private Simulation sim;
2324

24-
@Autowired
25-
private ModelMapper mapper;
25+
private SettlementMapper mapper = Mappers.getMapper(SettlementMapper.class);
2626

2727
@GetMapping("")
2828
List<SettlementSummaryDTO> getSettlements() {
29-
return sim.getSettlements().stream().map(s -> mapper.map(s, SettlementSummaryDTO.class)).collect(Collectors.toList());
29+
return sim.getSettlements().stream().map(s -> mapper.toSummary(s)).collect(Collectors.toList());
3030
}
3131

3232
@GetMapping("/{id}")
3333
public SettlementDTO getSettlement(@PathVariable int id) {
3434
Settlement s = sim.getSettlements().get(id-1);
35-
return mapper.map(s, SettlementDTO.class);
35+
return mapper.toFull(s);
3636
}
3737
}

rest-server/src/main/java/org/mars_sim/rest/dto/PersonDTO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
@Getter @Setter
77
public class PersonDTO extends PersonSummaryDTO {
8+
89
private ReferencableDTO settlement;
10+
11+
912
private ReportingAuthorityDTO authority;
1013
}

rest-server/src/main/java/org/mars_sim/rest/dto/PersonSummaryDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ public class PersonSummaryDTO extends ReferencableDTO {
1212
private String role;
1313
private String gender;
1414
private String building;
15+
1516
private String conditionHealth;
1617
private float conditionPerformance;
1718
private float conditionEnergy;
19+
1820
private String task;
1921
}

rest-server/src/main/java/org/mars_sim/rest/dto/ReferencableDTO.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.mars_sim.rest.dto;
22

3+
34
import lombok.Getter;
45
import lombok.Setter;
56

@@ -8,7 +9,10 @@
89
*/
910
@Getter @Setter
1011
public class ReferencableDTO {
12+
1113
private String name;
14+
1215
private String type;
16+
1317
private int identifier;
1418
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.mars_sim.rest.mapper;
2+
3+
import org.mapstruct.InheritConfiguration;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.Mapping;
6+
import org.mars_sim.rest.dto.PersonDTO;
7+
import org.mars_sim.rest.dto.PersonSummaryDTO;
8+
import org.mars_sim.rest.model.Person;
9+
10+
11+
@Mapper
12+
public interface PersonMapper {
13+
@Mapping(target = "conditionPerformance", source="condition.performance")
14+
@Mapping(target = "conditionEnergy", source="condition.energy")
15+
@Mapping(target = "conditionHealth", source="condition.health")
16+
PersonSummaryDTO toSummary(Person source);
17+
18+
@InheritConfiguration(name = "toSummary")
19+
PersonDTO toFull(Person source);
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.mars_sim.rest.mapper;
2+
3+
import org.mapstruct.InheritConfiguration;
4+
import org.mapstruct.Mapper;
5+
import org.mars_sim.rest.dto.SettlementDTO;
6+
import org.mars_sim.rest.dto.SettlementSummaryDTO;
7+
import org.mars_sim.rest.model.Settlement;
8+
9+
@Mapper
10+
public interface SettlementMapper {
11+
SettlementSummaryDTO toSummary(Settlement source);
12+
13+
@InheritConfiguration(name = "toSummary")
14+
SettlementDTO toFull(Settlement source);
15+
}

0 commit comments

Comments
 (0)