├── src └── main │ ├── resources │ └── application.properties │ └── java │ └── com │ └── epam │ └── mapstruct │ ├── dao │ ├── model │ │ ├── Location.java │ │ ├── AddressEntity.java │ │ └── UserEntity.java │ └── impl │ │ └── UserRepository.java │ ├── service │ ├── dto │ │ ├── LocationDTO.java │ │ ├── AddressDTO.java │ │ ├── FlattenUserDTO.java │ │ └── UserDTO.java │ ├── mapper │ │ └── UserMapper.java │ └── impl │ │ └── UserService.java │ ├── MapstructDemoApplication.java │ └── web │ └── controller │ └── UserController.java ├── .gitignore ├── README.md └── pom.xml /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=80 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/pom.xml.versionsBackup 2 | **/target/ 3 | .idea/ 4 | *.iml 5 | *.classpath 6 | *.project -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/dao/model/Location.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.dao.model; 2 | 3 | /** 4 | * Created by Adam_Skowron on 3/17/2017. 5 | */ 6 | public enum Location { 7 | NOVIGRAD, 8 | TEMERIA, 9 | REDANIA, 10 | RIVIA, 11 | UNKNOWN 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/service/dto/LocationDTO.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.service.dto; 2 | 3 | /** 4 | * Created by Adam_Skowron on 3/17/2017. 5 | */ 6 | public enum LocationDTO { 7 | NOVIGRAD, 8 | TEMERIA, 9 | REDANIA, 10 | RIVIA, 11 | UNKNOWN 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/service/dto/AddressDTO.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.service.dto; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * Created by Adam_Skowron on 3/10/2017. 7 | */ 8 | @Data 9 | public class AddressDTO { 10 | 11 | private LocationDTO locationType; 12 | 13 | private String location; 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/service/dto/FlattenUserDTO.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.service.dto; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * Created by Adam_Skowron on 3/10/2017. 7 | */ 8 | @Data 9 | public class FlattenUserDTO { 10 | 11 | private String firstName; 12 | 13 | private String lastName; 14 | 15 | private String street; 16 | 17 | private String location; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/MapstructDemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class MapstructDemoApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(MapstructDemoApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/service/dto/UserDTO.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.service.dto; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * Created by Adam_Skowron on 3/10/2017. 7 | */ 8 | @Data 9 | public class UserDTO { 10 | 11 | private long id; 12 | 13 | private String username; 14 | 15 | private String firstName; 16 | 17 | private String lastName; 18 | 19 | private AddressDTO address; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring boot + maven + mapstruct 1.2.0.Beta2 + Lombok 2 | =============== 3 | 4 | *Description*: Just the REST Spring Boot demo project with a maven configuration of mapstruct and lombok to work together. 5 | 6 | *Features checked*: 7 | * Automatic creation of sub-mapping methods 8 | * Automatic creation of enum mapping methods 9 | * Two approaches to map List - via .map() inside stream, or mapping entire list via standard iterator 10 | * unmappedTargetPolicy = ReportingPolicy.IGNORE 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/dao/model/AddressEntity.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.dao.model; 2 | 3 | /** 4 | * Created by Adam_Skowron on 3/10/2017. 5 | */ 6 | public class AddressEntity { 7 | 8 | private Location locationType; 9 | 10 | private String location; 11 | 12 | public AddressEntity(String location, Location locationType) { 13 | this.location = location; 14 | this.locationType = locationType; 15 | } 16 | 17 | public Location getLocationType() { 18 | return locationType; 19 | } 20 | 21 | public void setLocationType(Location locationType) { 22 | this.locationType = locationType; 23 | } 24 | 25 | public String getLocation() { 26 | return location; 27 | } 28 | 29 | public void setLocation(String location) { 30 | this.location = location; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/service/mapper/UserMapper.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.service.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.mapstruct.Mapper; 6 | import org.mapstruct.Mapping; 7 | import org.mapstruct.Mappings; 8 | import org.mapstruct.ReportingPolicy; 9 | 10 | import com.epam.mapstruct.dao.model.UserEntity; 11 | import com.epam.mapstruct.service.dto.FlattenUserDTO; 12 | import com.epam.mapstruct.service.dto.UserDTO; 13 | 14 | /** 15 | * Created by Adam_Skowron on 3/10/2017. 16 | */ 17 | @Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) 18 | public interface UserMapper { 19 | 20 | List mapToUsers(List userEntityList); 21 | 22 | @Mappings({ 23 | @Mapping(target = "location", source = "address.location") 24 | }) 25 | FlattenUserDTO mapToFlattenUser(UserEntity userEntity); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/service/impl/UserService.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.service.impl; 2 | 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import com.epam.mapstruct.dao.impl.UserRepository; 10 | import com.epam.mapstruct.service.dto.FlattenUserDTO; 11 | import com.epam.mapstruct.service.dto.UserDTO; 12 | import com.epam.mapstruct.service.mapper.UserMapper; 13 | 14 | /** 15 | * Created by Adam_Skowron on 3/10/2017. 16 | */ 17 | @Service 18 | public class UserService { 19 | 20 | private UserRepository userRepository; 21 | 22 | private UserMapper userMapper; 23 | 24 | @Autowired 25 | public UserService(UserRepository userRepository, UserMapper userMapper) { 26 | this.userRepository = userRepository; 27 | this.userMapper = userMapper; 28 | } 29 | 30 | public List getUsers() { 31 | return userMapper.mapToUsers(userRepository.getUsers()); 32 | } 33 | 34 | public List getFlattenUsers() { 35 | return userRepository.getUsers() 36 | .stream() 37 | .map(user -> userMapper.mapToFlattenUser(user)) 38 | .collect(Collectors.toList()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/web/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.web.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.http.HttpStatus; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.RequestMapping; 9 | import org.springframework.web.bind.annotation.RequestMethod; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | import com.epam.mapstruct.service.dto.FlattenUserDTO; 13 | import com.epam.mapstruct.service.dto.UserDTO; 14 | import com.epam.mapstruct.service.impl.UserService; 15 | 16 | /** 17 | * Created by Adam_Skowron on 3/10/2017. 18 | */ 19 | @RestController 20 | @RequestMapping("users") 21 | public class UserController { 22 | 23 | private UserService userService; 24 | 25 | @Autowired 26 | public UserController(UserService userService) { 27 | this.userService = userService; 28 | } 29 | 30 | @RequestMapping(value = "", method = RequestMethod.GET) 31 | public ResponseEntity> getUsers() { 32 | return new ResponseEntity>(userService.getUsers(), HttpStatus.OK); 33 | } 34 | 35 | @RequestMapping(value = "/flattenList", method = RequestMethod.GET) 36 | public ResponseEntity> getFlattenUsers() { 37 | return new ResponseEntity>(userService.getFlattenUsers(), HttpStatus.OK); 38 | } 39 | 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/dao/model/UserEntity.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.dao.model; 2 | 3 | /** 4 | * Created by Adam_Skowron on 3/10/2017. 5 | */ 6 | public class UserEntity { 7 | 8 | private long id; 9 | 10 | private String username; 11 | 12 | private String firstName; 13 | 14 | private String lastName; 15 | 16 | private AddressEntity address; 17 | 18 | public UserEntity(long id, String username, String firstName, String lastName, AddressEntity address) { 19 | this.id = id; 20 | this.username = username; 21 | this.firstName = firstName; 22 | this.lastName = lastName; 23 | this.address = address; 24 | } 25 | 26 | public long getId() { 27 | return id; 28 | } 29 | 30 | public void setId(long id) { 31 | this.id = id; 32 | } 33 | 34 | public String getUsername() { 35 | return username; 36 | } 37 | 38 | public void setUsername(String username) { 39 | this.username = username; 40 | } 41 | 42 | public String getFirstName() { 43 | return firstName; 44 | } 45 | 46 | public void setFirstName(String firstName) { 47 | this.firstName = firstName; 48 | } 49 | 50 | public String getLastName() { 51 | return lastName; 52 | } 53 | 54 | public void setLastName(String lastName) { 55 | this.lastName = lastName; 56 | } 57 | 58 | public AddressEntity getAddress() { 59 | return address; 60 | } 61 | 62 | public void setAddress(AddressEntity address) { 63 | this.address = address; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/epam/mapstruct/dao/impl/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.epam.mapstruct.dao.impl; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.springframework.stereotype.Repository; 7 | 8 | import com.epam.mapstruct.dao.model.AddressEntity; 9 | import com.epam.mapstruct.dao.model.Location; 10 | import com.epam.mapstruct.dao.model.UserEntity; 11 | 12 | /** 13 | * Created by Adam_Skowron on 3/10/2017. 14 | */ 15 | @Repository 16 | public class UserRepository { 17 | 18 | public List getUsers() { 19 | List userEntities = new ArrayList<>(); 20 | userEntities.add(new UserEntity(1L, "olgierd", "Olgierd", "von Everec", 21 | new AddressEntity("Redania", Location.REDANIA))); 22 | userEntities.add(new UserEntity(2L, "letho", "Letho", "Gulet", 23 | new AddressEntity("Gulet", Location.UNKNOWN))); 24 | userEntities.add(new UserEntity(3L, "john", "John", "Natalis", 25 | new AddressEntity("Novigrad", Location.NOVIGRAD))); 26 | userEntities.add(new UserEntity(4L, "geralt", "Geralt", "z Rivii", 27 | new AddressEntity( "Rivia", Location.RIVIA))); 28 | userEntities.add(new UserEntity(5L, "yennefer", "Yennefer", "z Vengerbergu", 29 | new AddressEntity("Vengerbeg", Location.UNKNOWN))); 30 | userEntities.add(new UserEntity(6L, "triss", "Triss", "Merigold", 31 | new AddressEntity("Novigrad", Location.NOVIGRAD))); 32 | userEntities.add(new UserEntity(7L, "jaskier", "Jaskier", "", 33 | new AddressEntity("Novigrad", Location.NOVIGRAD))); 34 | userEntities.add(new UserEntity(8L, "zoltan", "Zoltan", "Chivay", 35 | new AddressEntity("Novigrad", Location.NOVIGRAD))); 36 | userEntities.add(new UserEntity(9L, "radowid", "Radowid", "The King", 37 | new AddressEntity("Redania", Location.REDANIA))); 38 | userEntities.add(new UserEntity(10L, "emhyr", "Emhyr", "var Emreis", 39 | new AddressEntity( "Novigrad", Location.NOVIGRAD))); 40 | userEntities.add(new UserEntity(11L, "ciri", "Cirilla", "var Emreis", 41 | new AddressEntity( "Novigrad", Location.NOVIGRAD))); 42 | userEntities.add(new UserEntity(12L, "dijkstra", "Sigismund", "Dijkstra", 43 | new AddressEntity("Novigrad", Location.NOVIGRAD))); 44 | userEntities.add(new UserEntity(13L, "vesemir", "Vesemir", "", 45 | new AddressEntity("Karen", Location.RIVIA))); 46 | userEntities.add(new UserEntity(14L, "lambert", "Lambert", "", 47 | new AddressEntity("Karen", Location.RIVIA))); 48 | userEntities.add(new UserEntity(15L, "gaunter o'dimm", "Gaunter", "O'Dimm", 49 | new AddressEntity( "", Location.UNKNOWN))); 50 | userEntities.add(new UserEntity(16L, "yarpen", "Yarpen", "Zigrin", 51 | new AddressEntity("Novigrad", Location.NOVIGRAD))); 52 | userEntities.add(new UserEntity(17L, "foltest", "Foltest", "The king", 53 | new AddressEntity("Temeria", Location.TEMERIA))); 54 | userEntities.add(new UserEntity(18L, "keira", "Keira", "Metz", 55 | new AddressEntity("Carreras", Location.UNKNOWN))); 56 | userEntities.add(new UserEntity(19L, "ves", "Ves", "", 57 | new AddressEntity("Temeria", Location.TEMERIA))); 58 | userEntities.add(new UserEntity(20L, "vernon", "Vernon", "Roche", 59 | new AddressEntity("Temeria", Location.TEMERIA))); 60 | return userEntities; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.epam.mapstruct 8 | mapstruct-demo 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | mapstruct-demo 13 | Demo project for Spring Boot 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 1.5.2.RELEASE 19 | 20 | 21 | 22 | 23 | UTF-8 24 | UTF-8 25 | 1.8 26 | 1.2.0.Beta2 27 | 1.16.14 28 | 29 | 30 | 31 | 32 | org.springframework.boot 33 | spring-boot-starter-web 34 | 35 | 36 | 37 | org.springframework.boot 38 | spring-boot-starter-test 39 | test 40 | 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | ${org.projectlombok.version} 46 | 47 | 48 | 49 | org.mapstruct 50 | mapstruct 51 | ${org.mapstruct.version} 52 | 53 | 54 | 55 | org.mapstruct 56 | mapstruct-jdk8 57 | ${org.mapstruct.version} 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-compiler-plugin 72 | 3.6.0 73 | 74 | ${java.version} 75 | ${java.version} 76 | 77 | 78 | org.projectlombok 79 | lombok 80 | ${org.projectlombok.version} 81 | 82 | 83 | org.mapstruct 84 | mapstruct-processor 85 | ${org.mapstruct.version} 86 | 87 | 88 | 89 | -Amapstruct.suppressGeneratorTimestamp=true 90 | -Amapstruct.defaultComponentModel=spring 91 | 92 | 93 | 94 | 95 | 96 | 97 | --------------------------------------------------------------------------------