Welcome! Type "help" for available commands.
$
Loading terminal interface...
Back to Blog

Setting Up a Modern Spring Boot Web Server with REST API & Web Content (2025 Guide)

May 12, 2025
William Callahan

Software engineer, founder, and leadership background in finance/tech. Based in San Francisco.

spring bootjavamavenintellij idearest apiweb developmentbackendtutorial
Setting Up a Modern Spring Boot Web Server with REST API & Web Content (2025 Guide)

Related Java Setup Guides

  • Core Guide: Java, Maven & Gradle Setup & Usage
  • Specifics: Setting up JavaFX
  • This Article: Specifics for Setting up a Spring Boot Server

Overview

This guide covers creating a Spring Boot application. Key aspects:

  • Build Tool Setup (Maven/Gradle): JDK, Maven, and Gradle setup (including JAVA_HOME/M2_HOME) are covered in the Core Java & Build Tools Setup Guide. While this article primarily demonstrates a Maven-based Spring Boot project, Gradle is a well-supported alternative, and the linked guide provides details for both.
  • IntelliJ IDEA Spring Initializr: For easy project generation.
  • REST API Endpoint: A simple "Hello World" example.
  • Static Web Content: Serving HTML/CSS/JS.
  • Application Configuration: Using application.properties and environment variables.

Part 1: Create a Spring Boot Project

Toggle dropdownProject structure
  • src/main/java: Application code (MySpringBootAppApplication.java).
  • src/main/resources/static: Static assets (HTML, CSS, JS).
  • src/main/resources/application.properties: Configuration for the application.
  • pom.xml: Maven configuration and dependency management.

Part 2: Application Configuration

Toggle dropdownapplication.properties setup

Edit src/main/resources/application.properties to configure your application:

# Server configuration
server.port=8080
server.servlet.context-path=/api

# Logging configuration
logging.level.root=INFO
logging.level.com.example=DEBUG

# Spring devtools
spring.devtools.restart.enabled=true

For different environments, create profile-specific properties:

  • application-dev.properties
  • application-prod.properties
Toggle dropdownEnvironment variables with .env

For sensitive data like API keys, use environment variables.

  1. Add spring-dotenv dependency to pom.xml:
<dependency>
    <groupId>me.paulschwarz</groupId>
    <artifactId>spring-dotenv</artifactId>
    <version>4.0.0</version>
</dependency>
  1. Create .env.example file:
SERVER_PORT=8080
CUSTOM_API_KEY=your-api-key-here
  1. Create your actual .env file (don't commit to git):
SERVER_PORT=8080
CUSTOM_API_KEY=actual-api-key-value
  1. Reference env variables in application.properties:
server.port=${SERVER_PORT:8080}
api.key=${CUSTOM_API_KEY}

Part 3: Build a Basic REST API

Toggle dropdownHelloController.java

Create src/main/java/com/example/myspringbootapp/HelloController.java:

package com.example.myspringbootapp;

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

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String sayHello(
      @RequestParam(defaultValue = "World") String name
    ) {
        return String.format("Hello, %s!", name);
    }
}
Toggle dropdownRun and test
  • Run in IntelliJ: click the green play icon next to main method
  • CLI:
    mvn spring-boot:run
    

Visit http://localhost:8080/hello and http://localhost:8080/hello?name=Spring or use:

curl http://localhost:8080/hello
curl http://localhost:8080/hello?name=Developer

Part 4: Serve Static Web Content

Toggle dropdownStatic index.html
  1. Create src/main/resources/static/index.html:
    <!DOCTYPE html>
    <html lang="en">
    <head><meta charset="UTF-8"><title>Static Page</title></head>
    <body>
      <h1>Welcome to My Static Page!</h1>
    </body>
    </html>
    
  2. Restart app and browse to http://localhost:8080/ Spring Boot serves files in static/ automatically.

Part 5: Essential Maven Commands for Spring Boot

Refer to the Core Maven Guide for general Maven commands. Specific to Spring Boot (using the Spring Boot Maven Plugin, typically included):

Toggle dropdownSpring Boot Maven Plugin Commands
# Run the Spring Boot application (dev mode with hot reload if DevTools is present)
./mvnw spring-boot:run

# Package the application into an executable JAR
./mvnw clean package
# (The spring-boot-maven-plugin's repackage goal runs by default during the package phase)

# Build an OCI image (e.g., Docker image) if configured
./mvnw spring-boot:build-image

The spring-boot:run goal is particularly useful for development. For other standard Maven lifecycle commands like clean, compile, test, install, use them as described in the Core Maven Guide.

Part 6: Development & Hot Reload

Toggle dropdownHot reload setup with Spring Boot DevTools

Spring Boot DevTools (spring-boot-devtools dependency) enables automatic restart/hot reload by default when files change. To optimize:

  1. For IntelliJ IDEA:

    • In Preferences > Build, Execution, Deployment > Compiler, enable "Build project automatically"
    • Enable advanced settings: Registry (Ctrl+Alt+Shift+/) > check "compiler.automake.allow.when.app.running"
  2. For CLI:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.devtools.restart.enabled=true"
  1. To run with specific profile:
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev # For Maven
# For Gradle: ./gradlew bootRun --args='--spring.profiles.active=dev'

Part 7: Key Spring Boot Concepts & Troubleshooting

Toggle dropdownSpring Boot Auto-configuration

Spring Boot automatically configures your application based on the JAR dependencies you have added. For example, if spring-boot-starter-web is on the classpath, it auto-configures Tomcat and Spring MVC.

Toggle dropdownStarters

Spring Boot "Starters" are convenient dependency descriptors that you can include in your application. For example, spring-boot-starter-data-jpa for JPA/Hibernate, spring-boot-starter-security for security.

Toggle dropdownPort Conflict

If port 8080 is in use, change it in application.properties (e.g., server.port=8081) or via command line:

./mvnw spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"
Toggle dropdownMaven Central Repository

Maven Central (central.sonatype.com) is the primary public repository for Java libraries (dependencies). Spring Boot dependencies are typically fetched from here or other configured repositories (like Spring Milestones/Snapshots).

Part 8: Deployment Options

Toggle dropdownPackage for Production (Executable JAR)
# Using Maven
./mvnw clean package
# This creates an executable JAR in target/your-app-name.jar

# Using Gradle
./gradlew clean bootJar
# This creates an executable JAR in build/libs/your-app-name.jar
Run the JAR: java -jar target/myspringbootapp-0.0.1-SNAPSHOT.jar (adjust filename).
Toggle dropdownRunning with Different Profiles in Production
java -jar target/*.jar --spring.profiles.active=prod

When running in production, set environment variables externally:

export SERVER_PORT=80
export CUSTOM_API_KEY=production-key
java -jar target/*.jar
Toggle dropdownDocker deployment
  1. Create Dockerfile:
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
  1. Build and run:
docker build -t myspringbootapp .
docker run -p 8080:8080 --env-file .env myspringbootapp

Similar Content

HomeExperienceEducationCVProjectsBookmarksInvestmentsContactBlog
Welcome! Type "help" for available commands.
$
Loading terminal interface...

Similar Content

Related Articles

BLOG
April 8, 2025
How to setup the Java SDK and use JavaFX with macOS/Windows

How to setup the Java SDK and use JavaFX with macOS/Windows

A modern guide to setting up Java development with JavaFX on macOS and Windows using IntelliJ IDEA or VS Code. While setting `JAVA_HOME` is still usef...

macosjavazsh+12 more
William CallahanWilliam Callahan
BLOG
May 17, 2025
Java, Maven, & Gradle: OS Setup & Project-Level Usage

Java, Maven, & Gradle: OS Setup & Project-Level Usage

Install Java, Maven, Gradle at OS & project levels. Learn common commands for consistent builds & development workflows.

javajdkmaven+14 more
William CallahanWilliam Callahan
BLOG
May 7, 2025
Kickstarting a Modern Django Project (using UV) in 2025

Kickstarting a Modern Django Project (using UV) in 2025

A comprehensive guide to rapidly launching a Django project with modern templates, SaaS core features, SQL database integration, and popular IDE tooli...

djangopythonvscode+16 more
William CallahanWilliam Callahan

Related Bookmarks

LINK
September 14, 2025
GitHub - ChangeNode/spring-boot-supabase: Modern Java web application starter template.

GitHub - ChangeNode/spring-boot-supabase: Modern Java web application starter template.

Modern Java web application starter template. Contribute to ChangeNode/spring-boot-supabase development by creating an account on GitHub.

authentication solutionsgithub projectssupabase integrations+10 more
github.com
LINK
June 17, 2025
Linter Plugins

Linter Plugins

An overview of how to use Biome’s Linter Plugins

code quality toolsjavascript linterslinter plugins+3 more
biomejs.dev
LINK
June 22, 2025
The Type Safe TypeScript API Framework

The Type Safe TypeScript API Framework

Spiceflow is a lightweight, type-safe API framework for building web services using modern web standards.

type-safe apisopenapi integrationtypescript frameworks+10 more
getspiceflow.com

Related Investments

INV
December 31, 2020
Polly

Polly

Polly is a modern employee feedback and engagement platform.

enterpriseseries a+active+6 more
aVenture
INV
December 31, 2020
Stan

Stan

Platform enabling creators to monetize their content and engage with fans through personalized experiences.

saasseedactive+8 more
INV
December 31, 2022
AngelList

AngelList

Platform connecting startups with investors, talent, and resources for fundraising and growth.

investment platformsotheractive+8 more
aVenture

Related Projects

PRJ
Book Finder (findmybook.net)

Book Finder (findmybook.net)

Book search and recommendation engine with OpenAI integration

book searchbook finderbook recommendation+9 more
PRJ
repo-tokens-calculator

repo-tokens-calculator

CLI token counter (Python + tiktoken + uv) with pretty summary

clipythontiktoken+11 more
PRJ
SearchAI

SearchAI

AI-powered web search with a contextual chat assistant

aiweb searchchat assistant+14 more