Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Creating a simple Spring Boot project is made easy through the use of the Spring Initializr web service. Here's a step-by-step guide to setting up a basic Spring Boot project:
Maven
(most common) or Gradle
.Java
(most common), Kotlin
, or Groovy
.com.example
).myspringbootapp
).Jar
(common for microservices) or War
(for traditional Java EE servers).Spring Web
under the "Web" section.Generate
to download a .zip
file containing your new project..zip
file you downloaded.The structure of a Spring Boot project created by Spring Initializr is:
src/main/java
: Contains the main application file with the @SpringBootApplication
annotation and any other Java classes you add.src/main/resources
: Contains configuration files like application.properties
or application.yml
and static or template resources.src/test/java
: Contains test classes.If you added the Spring Web
dependency, Spring Boot includes an embedded Tomcat server, so you can run your application as a standalone Java application.
YourProjectNameApplication.java
).Your Spring Boot application will now start, and you can access it on http://localhost:8080/
by default. If you haven't added any controllers yet, accessing this URL will result in an error. But don't worry, this means your application is up and running!
In the src/main/java
directory, under your main package, create a new class named HelloController
.
package com.example.myspringbootapp; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/") public String hello() { return "Hello, Spring Boot!"; } }
Run your application again and access http://localhost:8080/
to see the greeting.
This is the basic structure and setup of a Spring Boot project. From here, you can add more dependencies, create entities, services, repositories, and so on as your project requires.
Creating a RESTful service in Spring Boot for beginners:
@RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } }
Configuring a simple web application in Spring Boot:
@SpringBootApplication public class SimpleWebApplication { public static void main(String[] args) { SpringApplication.run(SimpleWebApplication.class, args); } }
Setting up Spring Boot project structure for simplicity:
src �� ������ main ������ java �� ������ com �� ������ example �� ������ SimpleWebApplication.java ������ resources ������ application.properties
Adding dependencies to a simple Spring Boot project:
spring-boot-starter-web
, in your pom.xml
.<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Configuring application properties in a basic Spring Boot app:
application.properties
.server.port=8080
Creating and running a simple Spring Boot main class:
@SpringBootApplication public class SimpleBootApplication { public static void main(String[] args) { SpringApplication.run(SimpleBootApplication.class, args); } }
Basic controller and request mapping in Spring Boot:
@RestController public class SimpleController { @GetMapping("/welcome") public String welcome() { return "Welcome to Spring Boot!"; } }
Configuring logging for a straightforward Spring Boot project:
application.properties
.logging.level.root=info
Securing a simple Spring Boot application for beginners:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/public/**").permitAll() .anyRequest().authenticated() .and().formLogin().permitAll() .and().logout().permitAll(); } }