Spring Boot
Overview
Spring Boot is a Java framework that makes it easy to create stand-alone, production-grade applications. It provides auto-configuration, embedded servers, and an opinionated approach to building REST APIs and web applications.
Quick Start
Create Repository from Template
On your dashboard, go to the Tools & Deployment tab. Click the "New Repository" button, select the Spring Boot template from the dropdown, enter a repository name and subdomain, then click "Create Repository".
Clone Your Repository
git clone https://<git-server>/<username>/<repo-name>.git
cd <repo-name>
Configure Database
Update src/main/resources/application.properties with your dashboard credentials:
spring.datasource.url=jdbc:mysql://db.<session-domain>:3306/<username>
spring.datasource.username=<username>
spring.datasource.password=<password>
Build and Run
./mvnw spring-boot:run
Server runs on http://localhost:8080 by default.
Push Your Changes
git add .
git commit -m "Initial setup"
git push origin main
Local Development with Docker
If you don't have Java installed locally, you can use Docker to build and run your Spring Boot application.
Running Commands with Docker
# Build the project
docker run --rm -v $(pwd):/app -w /app maven:3-eclipse-temurin-21 ./mvnw clean package -DskipTests
# Run the application
docker run --rm -v $(pwd):/app -w /app -p 8080:8080 eclipse-temurin:21 java -jar target/*.jar
Database Configuration
The NStrim platform provides you with a MySQL/MariaDB database. Configure your application.properties file with the correct credentials.
Full Configuration
spring.datasource.url=jdbc:mysql://db.<session-domain>:3306/<username>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
student01). Check your dashboard credentials section for exact values.Project Structure
├── src/
│ └── main/
│ ├── java/com/example/
│ │ ├── Application.java # Spring Boot entry point
│ │ ├── controllers/ # REST controllers
│ │ ├── models/ # JPA entities
│ │ ├── repositories/ # Spring Data repositories
│ │ └── services/ # Business logic
│ └── resources/
│ ├── application.properties # Configuration
│ ├── templates/ # Thymeleaf templates
│ └── static/ # CSS, JS, images
├── pom.xml # Maven dependencies
└── Dockerfile # Docker config
Common Tasks
Creating a REST Controller
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping
public List<User> getAll() {
return userRepository.findAll();
}
@PostMapping
public User create(@RequestBody User user) {
return userRepository.save(user);
}
}
Creating a JPA Entity
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
// getters and setters
}
Creating a Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
}
Useful Commands
./mvnw spring-boot:run
Start development server
./mvnw clean package
Build the project
./mvnw test
Run tests
./mvnw dependency:tree
Show dependency tree
Troubleshooting
Communications link failure / Connection refused
Cause: Database host is incorrect or not accessible.
Solution: Check that spring.datasource.url matches the database host from your dashboard.
Access denied for user
Cause: Wrong username or password.
Solution: Double-check spring.datasource.username and spring.datasource.password match your dashboard credentials exactly.
Port 8080 already in use
Solution: Add server.port=8081 to application.properties or stop the other process.
Whitelabel Error Page (404)
Cause: No controller mapped to the requested URL.
Solution: Ensure your controller has the correct @RequestMapping annotation and the application is scanning the right packages.