Java is an object - oriented programming language that follows the “write once, run anywhere” (WORA) principle. It has a rich set of libraries, a strong type system, and automatic memory management through garbage collection. Key concepts in Java include classes, objects, inheritance, polymorphism, and encapsulation.
// A simple Java class
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Spring Boot is designed to make it easy to create Spring - based applications. It uses convention over configuration, which means it has default settings that work well in most cases, reducing the amount of boilerplate code. Spring Boot applications are typically packaged as executable JARs and can be run independently.
JAVA_HOME
environment variable.The easiest way to start with Spring Boot is by using Spring Initializr. You can access it at https://start.spring.io/ . Here, you can select your project type (Maven or Gradle), Java version, and add dependencies.
A typical Spring Boot project has the following structure:
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ ├── controller
│ │ │ └── HelloController.java
│ │ └── service
│ └── resources
│ ├── application.properties
│ └── static
│ └── templates
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
Create a simple controller class to handle HTTP requests.
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
The @GetMapping
annotation is used to map HTTP GET requests to a specific method in the controller. You can also use other annotations like @PostMapping
, @PutMapping
, and @DeleteMapping
for different HTTP methods.
In a Spring Boot project using Maven, dependencies are managed in the pom.xml
file. For example, to add the Spring Web dependency:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring - boot - starter - web</artifactId>
</dependency>
</dependencies>
Spring Boot uses SLF4J for logging. You can configure logging levels in the application.properties
file.
logging.level.root=INFO
package com.example.demo.controller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(HelloController.class)
public class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testSayHello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, Spring Boot!"));
}
}
In this tutorial, we have covered the fundamental concepts of Java and Spring Boot, how to set up the environment, create a simple Spring Boot application, build RESTful APIs, and explored common and best practices. Spring Boot simplifies Java application development, allowing developers to focus on writing business logic rather than dealing with complex configurations. With the knowledge gained from this tutorial, you should be able to start building your own Spring Boot applications efficiently.