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:
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.