Search

Spring

🔍
Navigation

Requirements

📌
To follow this course, it is required to know following …
•
Basic Java and HTTP; a compatible JDK and Maven or Gradle.

Setup

Create a Java project with Spring Initializr and add Spring Web. Use the JDK and build-tool versions required by the selected Spring Boot release. With a Maven project, run ./mvnw spring-boot:run from the project directory.
Spring manages application objects and their dependencies. Spring Boot builds on Spring with auto-configuration and a convenient application setup. It reduces configuration work; it does not replace the underlying framework.

Request Flow

An HTTP request reaches a controller, which calls the application’s service logic. Keep persistence details behind a repository rather than putting every operation into the controller. These boundaries make changes easier to locate and test.
import java.util.Map; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController class HelloController { @GetMapping("/hello") Map<String, String> hello() { return Map.of("message", "Hello"); } }
Plain Text
복사
Add this class under the generated application’s package. A GET request to http://localhost:8080/hello returns a JSON message.

Dependency Injection

With constructor injection, an object declares what it needs and Spring supplies those dependencies. A service can depend on a repository interface without constructing the database implementation itself. Tests can then supply a substitute.

Request Validation

Check incoming values before changing application state. Validation rules describe acceptable input; database constraints protect stored data as well. Return an appropriate error rather than treating invalid input as a server failure.

Tips

•
Keep business logic out of controllers.
•
Use constructor injection for required dependencies.
•
Test valid input, invalid input, and failure responses.

QnA

Is Spring Boot a separate framework?
Why use constructor injection?
Does a successful startup prove the API works?

Error

404: check the HTTP method, path, and whether the controller is within component scanning. Startup failure: read the first relevant cause in the exception chain; a port conflict and a missing dependency require different fixes.

Reference