dir.by  
  Поиск  
Компьютер, программы
Java
Spring in Java (Spring Framework, Spring Data, Spring Boot, ...)
 Create a new Spring Web application (the request goes to the Controller → Controller creates a response as text or json) | Java, Spring Web, Spring Boot, Maven 
посмотрели 2349 раз
обновлено: 7 December 2025
Download an example:
MySpringWeb.zip ...
size: 20 kb
 
Note! You must have Java JDK installed. If you don't have it, then need to download and install Java JDK ...

Note! You must have IntelliJ IDEA Ultimate installed. If you don't have it, then need to download and install IntelliJ IDEA Ultimate ...

Note! You must have Tomcat Web Server installed. If you don't have it, then need to download and install Tomcat Web Server ...
Introduction Controller Routes
Example 1
We enter the address in Google Chrome
http://localhost:8080/home

web server response:

My Controller:
  Java  
@RestController
public class MyHomeController {

     @GetMapping("/home")
     public String myRoot() {
          return "Hello Evgen";
     }
}
Example 1
We enter the address in Google Chrome
http://localhost:8080/home

web server response:

My Controller:
  Java  
@RestController
public class MyHomeController {

     @GetMapping("/home")
     public String myRoot() {
          return "Hello Evgen";
     }
}
Step 1. Create a new project with the type Spring Boot
 
Select Spring Boots:
 
Since we have a simple project, do not check the boxes:
Step 2. Create a new MyHomeController.java file
Right-click on my package folder and create a new java file
 
Let's write the name MyHomeController
And choose the type Class
Press Enter
The file was created.
 
Inside the MyHomeController.java file, add the code:
package org.example.myspringweb;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyHomeController {

     @GetMapping("/home")
     public String myRoot() {
          return "Hello Evgen";
     }
}
Step 3. Add the spring-boot-starter-web library to the pom.xml file
This library is essential for running our application as a web server.
  Xml     File pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>3.4.4</version>
          <relativePath/> <!-- lookup parent from repository -->
     </parent>
     <groupId>org.example</groupId>
     <artifactId>MySpringWeb</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>MySpringWeb</name>
     <description>MySpringWeb</description>
     <url/>
     <licenses>
          <license/>
     </licenses>
     <developers>
          <developer/>
     </developers>
     <scm>
          <connection/>
          <developerConnection/>
          <tag/>
          <url/>
     </scm>
     <properties>
          <java.version>17</java.version>
     </properties>

     <dependencies>
          <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter</artifactId>
          </dependency>
          <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
          </dependency>

          <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-test</artifactId>
               <scope>test</scope>
          </dependency>
     </dependencies>

     <build>
          <plugins>
               <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
          </plugins>
     </build>

</project>
Attention!
After adding <dependency> ... </dependency> to the pom.xml file, this library will not be downloaded from the Internet.
To download this library from the Internet, you need to close the project and click open project

 
Step 4. Launch the project and see how it works
 
We see that our application is running:
1) Spring Boot is initialized when our application is launched (because we built our application with the Spring Boot type)
2) Next, Tomcat web server with port 8080 is started (because <dependency> ... spring-boot-starter-web ... </dependency> was added to the pom.xml file)
 
Our web server is running, here is its address:
http://localhost:8080

Let's run Google Chrome and enter the following address:
add at the end /hello
Just like that
http://localhost:8080/hello
We can see that our web application works well because there is an answer on the screen
Hello Evgen
Explanation of what Controller is and how it gives an answer
My example:
  Java  
@RestController
public class MyHomeController {

     @GetMapping("/home")
     public String myRoot() {
          return "Hello Evgen";
     }
}


We enter the address in Google Chrome http://localhost:8080/home

http://localhost:8080 This is a web server
/home This is a request

The web server must respond.
In Java Spring, the class and method give the answer.
Step 1
We specify which class will make a web answer.
In Java Spring, this class must have the @RestController attribute.
Step 2
We specify which method will make the web response.
In Java Spring, this method must have the @GetMapping attribute.
In the method with the @GetMapping("/home") attribute, we specify that the request /home will have a response.
 
← Previous topic
What is Dependency Injection in Spring? Creating a simple Spring project with Dependency Injection in the constructor with the attributes @Component, @Autowired | Editor IntelliJ Idea and language Java
 
Next topic →
Creating a new Spring WebSocket application (Java WebSocket sends a message to the JavaScript WebSocket) | Java, Spring WebSocket, Spring Boot, Maven
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

Экскурсии по Москве Экскурсии по Москве: пешеходные, автобусные и речные прогулки на любой вкус
Анонс! Ярмарка вакансий для молодежи, работа (учащихся, которые хотели бы подработать в свободное время, а также выпускники)|||Минск, Витебск, Гомель, Гродно, Могилев, Борисов, Полоцк, Брест, Барановичи, Пинск с 13 по 17 апреля 2026
  Объявления  
  Объявления  
 
Download and install
Installation JDK (download and install the Java library for Windows)
Download and install IntelliJ IDEA to explore Java, Spring, Jakarta EE | Functionality and differences: Community | Ultimate
Learn the language Java (class, interface, properties, etc.)
Create a simple console app in IntelliJ IDEA to learn Java
What is interface in Java ?
Directly in the code (at runtime) we make an implementation for the interface | Java
Threads in Java
lambda is an unnamed method (no name and no class owned) | Java
Exceptions and handling in Java. Example: try {...} catch (Exception e) {...}
Why use throws Exception in the function name in Java? Answer: The method throws an exception and there is no handling of try catch. Example: float CalculateDensity(float mass, float volume) throws Exception { ... throw new Exception("Error! Volume is zero."); ... }
Java web application (servlet, jsp page, basic functionality and no frameworks)
Creating a new simple web application (jsp web page) | Java, Maven
Create a web servlet (the servlet is located on the web server, returns the result on request) | Java
Installation Tomcat web server (download and install for Windows)
Check, test Tomcat web server (create a new my.hml file)
What is Maven
What is a dynamic web page jsp (Java Server Page) ?
How to install Smart Tomcat plugin in Intellij Idea
Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java
Spring in Java (Spring Framework, Spring Data, Spring Boot, ...)
What is Spring? Why use Spring in Java?
What is Spring Bean? What is Spring Container? IoC (inversion of control) | Creating a simple Spring project and configuration Spring Container in an XML file | Editor IntelliJ Idea and language Java
What is Dependency Injection in Spring? Creating a simple Spring project with Dependency Injection in the constructor with the attributes @Component, @Autowired | Editor IntelliJ Idea and language Java
Create a new Spring Web application (the request goes to the Controller → Controller creates a response as text or json) | Java, Spring Web, Spring Boot, Maven
Creating a new Spring WebSocket application (Java WebSocket sends a message to the JavaScript WebSocket) | Java, Spring WebSocket, Spring Boot, Maven
Create Azure Web App with the type Java (free). That is, create an empty web server
Put the Java Spring web application in Azure
Connect to the application Azure Web App (Java) via FTP | use File Explorer
Error "The SDK is not specified for module ... | Project SDK is not defined ..." in IntelliJ IDEA | Java, Spring Boot
How to install Spring WebSocket plugin in Intellij Idea
Jakarta EE / Java EE in Java (Web Applications, Web Services)
What is Jakarta EE (Java EE)? | In which editor (program) is it convenient to write Jakarta EE (Java EE) code?
Creating a simple web project in IntelliJ Idea Ultimate | Jakarta EE (Java EE)
Struts in Java (extends API Java Servlet using MVC)
What is Struts | Java
Interviews on Java

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика