“Time to show off your Spring Boot prowess—let’s launch your very own “Hello, World” API into the wild via Heroku!” 🚀🐒
🏁 Challenge: Build & Deploy Your Hello API
Follow these steps and get your service live in under 10 minutes.
1️⃣ Bootstrap Your Project
Use Spring Initializr (or your IDE):
- Dependencies: Spring Web
- Group:
com.example
- Artifact:
hello-app
Open the generated project in your IDE.
2️⃣ Add the Hello Controller
Create src/main/java/com/example/helloapp/HelloController.java
:
javaCopyEditpackage com.example.helloapp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "🐵 Hello, Heroku!";
}
}
3️⃣ Verify Locally
bashCopyEdit./mvnw clean package
./mvnw spring-boot:run
Visit http://localhost:8080/hello to see your greeting.
4️⃣ Prepare for Heroku
- Initialize Git bashCopyEdit
git init git add . git commit -m "Initial Spring Boot Hello API"
- Create Procfile at project root: bashCopyEdit
web: java -jar target/hello-app-0.0.1-SNAPSHOT.jar
- Add
system.properties
(to pin Java version): iniCopyEditjava.runtime.version=17
5️⃣ Deploy!
- Login & Create App bashCopyEdit
heroku login heroku create your-unique-app-name
- Push to Heroku bashCopyEdit
git push heroku main
- Scale Dynos (ensure at least one web dyno): bashCopyEdit
heroku ps:scale web=1
- Open in Browser bashCopyEdit
heroku open
or visithttps://your-unique-app-name.herokuapp.com/hello
6️⃣ Verify & Celebrate
You should see 🐵 Hello, Heroku! live on the internet. 🎉
🤔 Bonus Tips
- Custom Domain:
heroku domains:add www.yourdomain.com
- Environment Variables:
heroku config:set SPRING_PROFILES_ACTIVE=prod
- Logging:
heroku logs --tail
to debug in real time.