“Building a REST endpoint in Spring Boot is so quick, even a curious monkey could do it in five minutes!” 🐒✨

Let’s dive in—no fluff, just bite-sized steps to get a /greet API up and running.


⏱️ Prerequisites (1 minute)

  • You’ve got a Spring Boot project ready (see post #1 if not).
  • Java 17+ and Maven/Gradle installed.
  • IDE of your choice (IntelliJ, Eclipse, VS Code).

🛠️ 5-Minute Walkthrough

1. Create the Controller (30 s)

Under src/main/java/com/example/demo/, make a new file:

javaCopyEditpackage com.example.demo;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetController {
    // We’ll add methods here…
}
  • @RestController tells Spring: “This class holds REST endpoints.”

2. Define Your GET Endpoint (1 min)

Inside GreetController, add:

javaCopyEditimport org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@GetMapping("/greet")
public String greet(@RequestParam(defaultValue = "World") String name) {
    return "Hello, " + name + "! 🐵";
}
  • @GetMapping("/greet") → Maps HTTP GET /greet.
  • @RequestParam → Reads ?name=Alice, defaults to “World.”

Full class now:

javaCopyEditpackage com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetController {

    @GetMapping("/greet")
    public String greet(@RequestParam(defaultValue = "World") String name) {
        return "Hello, " + name + "! 🐵";
    }

}

3. Build & Run (1 min)

With Maven:

bashCopyEdit./mvnw clean package
./mvnw spring-boot:run

Or Gradle:

bashCopyEdit./gradlew clean build
./gradlew bootRun

Look for this in your logs:

nginxCopyEditStarted DemoApplication in 2.1 seconds…

4. Test Your Endpoint (1 min)

Curl test:

bashCopyEdit# Default greeting
curl http://localhost:8080/greet
# → Hello, World! 🐵

# Custom name
curl "http://localhost:8080/greet?name=JavaFan"
# → Hello, JavaFan! 🐵

Or open http://localhost:8080/greet?name=JavaFan in your browser.


🧠 Monkey-Proof Tips

  • Keep controllers tiny: heavy logic belongs in a @Service class.
  • Return JSON: wrap your response in an object for richer data. javaCopyEditrecord GreetResponse(String message) {} @GetMapping("/greet") public GreetResponse greet(String name) { return new GreetResponse("Hello, " + name + "!"); }
  • Use ResponseEntity for status codes and headers.

🚀 Next Steps

  1. POST endpoint: Accept JSON bodies with @PostMapping and @RequestBody.
  2. Path variables: javaCopyEdit@GetMapping("/greet/{name}") public String greetPath(@PathVariable String name) { … }
  3. Error handling: Add @ControllerAdvice to handle exceptions globally.

🎯 Challenge

  • Extend this example:
    • Create a GreetService class, inject it into your controller, and move greeting logic there.
    • Add a POST /greet that accepts { "name": "YourName" } and returns { "message": "Hello, YourName!" }.
  • Bonus: Deploy your mini-API to Heroku or Railway.app in under 5 minutes!

👏 You just built your first Spring Boot REST endpoint in under 5 minutes. High-five, you backend champ! Next up: diving into modern front-end magic with React. Stay tuned!

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *