Back to Blog

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

May 12, 2025
William Callahan
William Callahan

Software engineer and entrepreneur based in San Francisco.

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

Overview

This guide covers setting up a modern Spring Boot application with:

  • Apache Maven for builds
  • IntelliJ IDEA Spring Initializr integration
  • A simple REST API endpoint
  • Serving static web content
  • Essential Maven commands and troubleshooting tips
  • Basic application configuration
  • Environment variables setup

Part 1: Prerequisites & Initial Setup

Install JDK (e.g., JDK 21 LTS)

brew install openjdk@21
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v21)' >> ~/.zshrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"'     >> ~/.zshrc
source ~/.zshrc
java -version

Install Apache Maven

# Extract Maven and then:
echo 'export M2_HOME=/opt/apache-maven-3.9.x' >> ~/.zshrc
echo 'export PATH="$M2_HOME/bin:$PATH"'   >> ~/.zshrc
source ~/.zshrc
mvn -v

Install IntelliJ IDEA

Download and install from https://www.jetbrains.com/idea/download/. Use Community Edition (free) or Ultimate (full Spring support).

Part 2: Create a Spring Boot Project

Generate project via CLI (start.spring.io)
Use curl to fetch and unpack a new Spring Boot starter:
curl https://start.spring.io/starter.zip \
  -d dependencies=web,devtools,thymeleaf \
  -d type=maven-project \
  -d language=java \
  -d javaVersion=21 \
  -d groupId=com.example \
  -d artifactId=myspringbootapp \
  -d name=MySpringBootApp \
  -d packageName=com.example.myspringbootapp \
  | tar -xzvf -
Project structure
  • src/main/java - Application code (MySpringBootAppApplication.java)
  • src/main/resources/static - Static assets (HTML, CSS, JS)
  • src/main/resources/application.properties - Configuration
  • pom.xml - Maven config and dependency management

Part 3: Application Configuration

application.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
Environment 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 4: Build a Basic REST API

HelloController.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);
    }
}
Run 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 5: Serve Static Web Content

Static 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 6: Essential Maven Commands

Maven commands
  • Build: mvn clean install
  • Run: mvn spring-boot:run
  • Test: mvn test
  • Dependencies: mvn dependency:tree

Part 7: Development & Hot Reload

Hot reload setup

Spring Boot DevTools enables hot reload by default. Optimize it with:

  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:
mvn spring-boot:run -Dspring.profiles.active=dev

Part 8: Key Concepts & Troubleshooting

Maven Central repository

Maven Central (central.sonatype.com) is the primary public repository for Java libraries and dependencies, serving a similar role to npm (JavaScript) or PyPI (Python) for the Java ecosystem.

Auto-configuration

Spring Boot auto-configures based on classpath; e.g., spring-boot-starter-web → embedded Tomcat.

Port conflict

Add server.port=8081 to application.properties or free port 8080.

Part 9: Deployment Options

Package for production
mvn clean package
java -jar target/*.jar
Running with different profiles
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
Docker 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