Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
Object Mapping - examples With Java Spring Boot

Object Mapping – examples With Java Spring Boot

Posted on March 21, 2025March 21, 2025 by Toma Velev

There are several Java Frameworks available that does Object Mapping – automatic creation of DTO (Data Transfer Object) in Spring Boot.

Reasons we need it

Spring (Boot) Framework saves a lot of time of developers by hiding complexity and placing smart defaults with convention over configuration. This comes with the price of a “magic” happening behind the scene that may create obsticles for you.

  • REST Endpoints – The very basic example is exposing the objects created by the Database layer directly to the REST. I’ll write more on how to REST in the future.
  • Using Frameworks like Google Web Toolkit https://www.gwtproject.org/, that take a portion of the Java SDK and compile it to JavaScript. Whenever there is something beyond it – it will now work out of the box.
  • Wrapping and Isolating out of external dependencies. It is general practice your business logic to not have dependencies on concrete implementations. This will make the code harder to change.

Object Mapping Options

Here are a few popular ones:

  1. ModelMapper: ModelMapper is a popular library for mapping between Java objects, including automatic DTO creation.
  2. Dozer: Dozer is another popular library for mapping between Java objects, including automatic DTO creation.
  3. Orika: Orika is a Java library that provides automatic DTO creation and mapping between Java objects.
  4. MapStruct: MapStruct is a Java library that provides automatic DTO creation and mapping between Java objects. It’s designed to be fast, efficient, and easy to use.
  5. Dozer-Mapstruct: Dozer-Mapstruct is a library that combines the features of Dozer and MapStruct to provide automatic DTO creation and mapping between Java objects.
  6. Spring Framework’s BeanUtils. Spring Framework provides a set of utility classes, including BeanUtils, that can be used to automatically create DTOs from Java objects.

Here’s an example of how you might use MapStruct to automatically create a DTO from a Java object:

MapStruct Example

MapStruct like Lombok – very much like many Spring Framework tools work with

  • processing your code,
  • finds annotated parts and
  • based on it – adds things on your classpath (at compile time).

The last step above is crucial for the tool to work. It does it with annotation post processor that needs to be activated by your IDE or build tool.

First, you need to add the following dependency in your pom.xml file if you’re using Maven:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.3.Final</version>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.5.3.Final</version>
</dependency>
 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>21</source>
<target>21</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
<!-- This is needed when using Lombok 1.18.16 and above -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<!-- Mapstruct should follow the lombok path(s) -->
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

Or in your build.gradle file if you’re using Gradle:

dependencies {
    implementation 'org.mapstruct:mapstruct:1.5.3.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.3.Final'
}

Here is the most basic code.

// Entity class
public class User {
    private Long id;
    private String name;

    // getters and setters
}

// DTO class
public class UserDTO {
    private Long id;
    private String name;

    // getters and setters
}

// Using MapStruct to create a DTO from an entity
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public UserDTO getUserDTO(User user) {
        return userMapper.map(user, UserDTO.class);
    }
}

You need to configure the Mapper in your application.

// UserMapperConfig.java

@MapperConfig
public class UserMapperConfig {
}

In The example above, the UserService class uses a mapping library to automatically create a UserDTO object from a User entity. This can save a lot of boilerplate code and make your application more maintainable.

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Feature Flags – Enable Functionality from the BackEnd
  • Integrating xAI Grok API with Spring Boot
  • How to Progresively Integrate AI
  • What is an AI Agent
  • Flutter image scaling

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (256)
  • Graphical User Interface (14)
  • Marketing (116)
  • Software Development (281)
  • Spring (44)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (14)