VOOZH about

URL: https://www.geeksforgeeks.org/java/data-transfer-object-dto-in-spring-mvc-with-example/

⇱ Data Transfer Object (DTO) in Spring MVC with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Data Transfer Object (DTO) in Spring MVC with Example

Last Updated : 1 Jun, 2026

In Spring Framework, a Data Transfer Object (DTO) is a simple Java object used to transfer data between different layers of an application. DTO helps reduce the number of method calls and allows data to move efficiently between the Controller, Service, and View layers.

  • DTO contains only data members with getter and setter methods
  • Helps transfer multiple values using a single object
  • Improves code readability, maintainability, and security
  • Commonly used with forms and model objects in Spring MVC

How DTO Work In Spring MVC

👁 2056958131
  • DTO collects data from the client (form or request) into a single Java object.
  • Spring automatically maps request parameters to DTO fields based on matching names.
  • The controller receives the filled DTO object instead of multiple parameters.
  • DTO is used to transfer data between View, Controller, and Service layers.
  • It makes data handling clean, organized, and easy to manage.

Steps to Implements a Data Transfer Object in Spring MVC

Follow these steps to implements Data Transfer Object in Spring Application.

Step 1: Create Maven Project

Open your STS IDE then create a new maven project, File > New > Maven Project, and choose the following archetype as shown in the below image as follows:  

👁 Image
 

Step 2: Add Maven Dependencies

Add the following maven dependencies and plugin to your pom.xml file. 

Step 3: Configure DispatcherServlet

In this step, we configure the DispatcherServlet, which acts as the front controller in Spring MVC and handles all incoming client requests. Before moving into the coding part let's have a look at the file structure in the below image. 

👁 Image
 

Step 4: Create Spring Initializer Class

create an src/main/java folder and inside this folder create a class named CalculatorAppIntilizer and put it inside the com.geeksforgeeks.calculator.config package and extends the AbstractAnnotationConfigDispatcherServletInitializer class. Refer to the below image.

👁 Image
 

Now we create CalculatorAppInitializer class inside the com.geeksforgeeks.calculator.config package and extend AbstractAnnotationConfigDispatcherServletInitializer.

Step 5: Create Spring Configuration Class

In this step, create the CalculatorAppConfig class to configure component scanning and register Spring MVC beans.

package com.geeksforgeeks.calculator.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.geeksforgeeks.calculator.controllers")
public class CalculatorAppConfig {


}

Step 6: Configure ViewResolver

In this step, configure InternalResourceViewResolver to map logical view names returned by the controller to JSP pages.

Step 7: Create Controller Class

Go to the src/main/java folder and inside this folder create a class named AppController and put it inside the com.geeksforgeeks.calculator.controllers package. Below is the code for the AppController.java file.

Step 8: Create JSP Form Page View

Now we have to create a view named "welcome-page" inside the WEB-INF/view folder with the .jsp extension. So go to the src > main > webapp > WEB-INF and create a folder view and inside that folder create a jsp file named welcome-page. So below is the code for the welcome-page.jsp file. 

The view is looking like this :

👁 Image

Step 9: Create DTO Class

At first, we have to create a DTO class. So go to the src/main/java folder and inside this folder create a class named NumberInfoDTO and put it inside the com.geeksforgeeks.calculator.dto package. Below is the code for the NumberInfoDTO.java file.

Step 10: Process DTO Data in Controller

So we have to create a controller with the "process-homepage" endpoint. So now come to the AppController.java file again and write down the following code inside this file.

@RequestMapping("/process-homepage")
public String showResultPage(NumberInfoDTO numberInfoDTO, Model model) {
// writing the value to the properties
// by fetching from the URL
model.addAttribute("numberInfo", numberInfoDTO);
return "result-page";
}

Below is the updated code for the AppController.java file.

Updated AppController.java file

Step 11: Create Result JSP Page

Now we have to create another view named "result-page" to display the captured values. So below is the code for the result-page.jsp file.

So now we have done with the coding part. Let's run and test our application. 

Step 12: Run and Test the Application

To run our Spring MVC Application right-click on your project > Run As > Run on Server. And run your application as shown in the below image as depicted below as follows:  

👁 Image

After that use the following URL to run your controller

http://localhost:8080/simple-calculator/geeksforgeeks.org/home

Output:

👁 Image

Enter values llike - 41 and 75 in the form and click Capture. After submission, Spring sends the request to the controller and generates this URL with query parameters:

http://localhost:8080/simple-calculator/geeksforgeeks.org/process-homepage?number1=41&number2=75

And you can see on the next page the values are displayed.

👁 Image

Explanation: When the user enters values (41 and 75) and clicks the Capture button, the form submits the data to the /process-homepage URL. Spring MVC automatically binds these request parameters to the NumberInfoDTO object. The controller then processes the DTO and displays the values on the result page.

Comment
Article Tags: