Spaces:
Sleeping
Sleeping
File size: 3,336 Bytes
4cdf2b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
---
title: Spring Boot API
emoji: π
colorFrom: green
colorTo: blue
sdk: docker
pinned: false
---
# Spring Boot API on Hugging Face Spaces
A Spring Boot REST API deployed on Hugging Face Spaces using Docker.
## Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/api/health` | Health check endpoint |
| GET | `/api/hello?name={name}` | Returns a greeting message |
| POST | `/api/echo` | Echoes back the JSON body |
## Example Usage
### Health Check
```bash
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/health
```
### Hello Endpoint
```bash
curl https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/hello?name=Developer
```
### Echo Endpoint
```bash
curl -X POST https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space/api/echo \
-H "Content-Type: application/json" \
-d '{"message": "Hello from Hugging Face!"}'
```
## Local Development
### Prerequisites
- Java 21 (or use the Maven wrapper which handles this)
- Maven 3.9+ (optional, wrapper included)
- Docker (optional, for container testing)
### Run with Maven
```bash
# On Linux/macOS
./mvnw spring-boot:run
# On Windows
mvnw.cmd spring-boot:run
```
The app will start on `http://localhost:7860`
### Build and Run with Docker
```bash
# Build the image
docker build -t springboot-hf .
# Run the container
docker run -p 7860:7860 springboot-hf
```
## Deploy to Hugging Face Spaces
### Option 1: Push Entire Backend Folder as a Space
1. Create a new Space on [Hugging Face](https://huggingface.co/new-space)
- Select **Docker** as the SDK
- Choose a name for your Space
2. Clone your new Space repository:
```bash
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
cd YOUR_SPACE_NAME
```
3. Copy the contents of this `backend` folder into the Space:
```bash
# Copy all files from backend folder
cp -r /path/to/backend/* .
```
4. Push to Hugging Face:
```bash
git add .
git commit -m "Initial Spring Boot deployment"
git push
```
### Option 2: Direct Git Push (if backend is standalone)
1. Add Hugging Face as a remote:
```bash
cd backend
git init # if not already a git repo
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
```
2. Push to Hugging Face:
```bash
git add .
git commit -m "Deploy Spring Boot to HF Spaces"
git push hf main
```
### Important Notes for Hugging Face Spaces
- The app **must** run on port **7860** (already configured)
- The container runs as a **non-root user** (UID 1000) - this is required by HF Spaces
- Build times may vary; initial deployment can take a few minutes
- Check the **Logs** tab in your Space for debugging
## Project Structure
```
backend/
βββ src/
β βββ main/
β β βββ java/com/backend/cs203/
β β β βββ Cs203Application.java # Main entry point
β β βββ resources/
β β βββ application.properties # App configuration
β βββ test/
βββ pom.xml # Maven dependencies
βββ Dockerfile # Multi-stage Docker build
βββ README.md # This file
```
## Tech Stack
- **Java 21**
- **Spring Boot 4.0.1**
- **Maven** (with wrapper)
- **Docker** (multi-stage build)
|