This quick Spring Boot tutorial shows how to expose a Hello endpoint using a REST controller so you can validate wiring, configuration and the fact that your computer still runs Java. You will use Spring Boot, Spring Web, Java and either Maven or Gradle to create a minimal web app that responds to HTTP requests.
Visit start.spring.io or use your IDE starter and include the Web dependency. Choose Maven or Gradle, pick group and artifact names and move on. This is a lab not a naming contest.
Add a tiny controller that handles GET requests at /hello
and returns a friendly greeting. Keep it small and obvious so debugging does not become a personality test.
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${greeting.message:Hello World}")
private String greeting;
@GetMapping("/hello")
public String hello() {
return greeting;
}
}
Start the app from your IDE by running the main class that calls SpringApplication.run. From the command line use the build tool command that feels right for you.
mvn spring-boot:run
./gradlew bootRun
Spring Boot listens on localhost
port 8080 by default unless you change server.port
in application.properties
or via environment settings.
Open a browser at http://localhost:8080/hello
or use curl and pretend you are a command line wizard.
curl http://localhost:8080/hello
# Expected output
Hello World
Move the greeting into src/main/resources/application.properties
so you can change messages without editing code. The controller above injects the value with @Value
and has a fallback of Hello World
so missing properties will not ruin your day.
# src/main/resources/application.properties
greeting.message=Hello from Spring Boot
If you want different text for different environments use profile specific properties like application-dev.properties
and set spring.profiles.active
. That is how professionals pretend to be organized.
There you go. A minimal Spring Boot web app, a REST controller, and a configurable hello message. Use Maven or Gradle as you prefer. Now go impress someone with the most basic but essential API you can build.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.