Skip to main content

Deploy Spring Boot

This guide will help you deploy a Spring Boot application to Deployxa.

Overview

Spring Boot is a Java framework for building production-ready applications. Deployxa provides optimized deployments for Spring Boot with automatic configuration.

Features:

  • Automatic framework detection
  • Java 17+ support
  • Maven and Gradle support
  • Database migrations
  • Automatic HTTPS
  • Global CDN

Prerequisites

  • A Spring Boot application (version 3.x recommended)
  • A GitHub repository with your code
  • A Deployxa account
  • PostgreSQL or MySQL database

Quick Start

1. Create Your Spring Boot App

If you don't have a Spring Boot app yet, use Spring Initializr:

Visit start.spring.io or use the CLI:

curl https://start.spring.io/starter.zip \
-d dependencies=web,data-jpa,postgresql \
-d type=maven-project \
-d language=java \
-d bootVersion=3.2.0 \
-o my-spring-boot-app.zip
unzip my-spring-boot-app.zip
cd my-spring-boot-app

2. Configure for Production

Update src/main/resources/application.properties:

spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
server.port=${PORT:8080}

3. Push to GitHub

git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/my-spring-boot-app.git
git push -u origin main

4. Deploy to Deployxa

  1. Go to Deployxa Dashboard
  2. Click "New Project"
  3. Select your GitHub repository
  4. Deployxa will automatically detect Spring Boot
  5. Click "Deploy"

Your app will be live in under 90 seconds!

Configuration

Detected Settings

Deployxa automatically detects these settings:

SettingValue
FrameworkSpring Boot
Build Command./mvnw clean package -DskipTests
Output Directorytarget/*.jar
Start Commandjava -jar target/*.jar
Port8080

Custom Configuration

If you need to customize settings:

  1. Go to ProjectSettingsBuild
  2. Modify settings as needed
  3. Save changes

Example Custom Settings:

{
"buildCommand": "./mvnw clean package -DskipTests",
"outputDirectory": "target",
"startCommand": "java -jar target/*.jar"
}

Environment Variables

Adding Environment Variables

  1. Go to ProjectEnvironment
  2. Click "Add Variable"
  3. Enter key and value
  4. Click "Save"

Common Spring Boot Environment Variables:

SPRING_PROFILES_ACTIVE=production
SERVER_PORT=8080

# Database
DATABASE_URL=jdbc:postgresql://host:5432/dbname
DB_USERNAME=your-username
DB_PASSWORD=your-password

# JPA
SPRING_JPA_HIBERNATE_DDL_AUTO=update
SPRING_JPA_SHOW_SQL=false

Using Environment Variables

In your Spring Boot app:

// application.properties
spring.datasource.url=${DATABASE_URL}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}

Or in Java code:

@Value("${DATABASE_URL}")
private String databaseUrl;

Database Configuration

Using PostgreSQL

Add dependency to pom.xml:

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

Configure in application.properties:

spring.datasource.url=${DATABASE_URL}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

Using MySQL

Add dependency to pom.xml:

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

Configure in application.properties:

spring.datasource.url=${DATABASE_URL}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

Database Migrations

Use Flyway for migrations:

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

Create migrations in src/main/resources/db/migration/:

-- V1__Create_users_table.sql
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
);

Custom Domains

Adding a Custom Domain

  1. Go to ProjectDomains
  2. Click "Add Domain"
  3. Enter your domain (e.g., example.com)
  4. Configure DNS records
  5. Wait for verification

DNS Configuration

For Apex Domain (example.com):

Type: A
Name: @
Value: 76.76.21.21

For Subdomain (www.example.com):

Type: CNAME
Name: www
Value: cname.deployxa.com

Performance Optimization

Enable Connection Pooling

Configure HikariCP (default):

spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000

Enable Caching

Add dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

Enable caching:

@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Use caching:

@Service
public class UserService {
@Cacheable("users")
public User getUser(Long id) {
return userRepository.findById(id).orElse(null);
}
}

Enable Compression

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain

Monitoring

Viewing Logs

  1. Go to ProjectLogs
  2. Select log type:
    • Build Logs: Deployment process
    • Runtime Logs: Application output
    • Error Logs: Application errors

Spring Boot Logging

Configure logging in application.properties:

logging.level.root=INFO
logging.level.com.example=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

Actuator Endpoints

Add dependency:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Configure in application.properties:

management.endpoints.web.exposure.include=health,info,metrics
management.endpoint.health.show-details=always

Access endpoints:

  • Health: https://your-app.deployxa.app/actuator/health
  • Info: https://your-app.deployxa.app/actuator/info
  • Metrics: https://your-app.deployxa.app/actuator/metrics

Monitoring Metrics

  1. Go to ProjectAnalytics
  2. View metrics:
    • Request count
    • Response times
    • Error rates
    • Bandwidth usage
    • CPU/RAM usage

Health Checks

Deployxa automatically monitors your application:

  • HTTP health checks every 30 seconds
  • Automatic restart on failure
  • Alerts on consecutive failures

Troubleshooting

500 Internal Server Error

Problem: Application returns 500 error

Solution:

  1. Check runtime logs for errors
  2. Verify environment variables
  3. Check database connection
  4. Ensure application starts correctly
  5. Check for missing dependencies

Database Connection Failed

Problem: Connection refused or Communications link failure

Solution:

  1. Verify DATABASE_URL is correct
  2. Check database host and port
  3. Ensure database exists
  4. Check firewall rules
  5. Verify database credentials

Build Failed

Problem: Maven or Gradle build fails

Solution:

  1. Check build logs for specific errors
  2. Verify dependencies are correct
  3. Check for syntax errors
  4. Test build locally
  5. Ensure Java version is compatible

Application Crashes on Start

Problem: Application crashes immediately

Solution:

  1. Check runtime logs for errors
  2. Verify environment variables
  3. Check for missing configuration
  4. Ensure PORT is configured correctly
  5. Check for port conflicts

Out of Memory

Problem: java.lang.OutOfMemoryError

Solution:

  1. Increase JVM heap size: java -Xmx512m -jar app.jar
  2. Optimize application code
  3. Upgrade plan for more RAM
  4. Check for memory leaks

Best Practices

Project Structure

my-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ ├── controller/
│ │ │ ├── service/
│ │ │ ├── repository/
│ │ │ ├── model/
│ │ │ └── Application.java
│ │ └── resources/
│ │ ├── application.properties
│ │ └── db/migration/
│ └── test/
├── pom.xml
├── .env.example
└── README.md

Environment Variables

  • Never commit .env file
  • Use .env.example as template
  • Rotate secrets regularly
  • Use different values per environment

Security

  • Keep Spring Boot updated
  • Use HTTPS only
  • Validate all inputs
  • Use parameterized queries
  • Enable CSRF protection
  • Use Spring Security
  • Keep dependencies updated

Performance

  • Use connection pooling
  • Enable caching
  • Optimize database queries
  • Use async processing
  • Enable compression
  • Use CDN for static assets

Database

  • Use migrations for schema changes
  • Index frequently queried columns
  • Use transactions for complex operations
  • Backup database regularly
  • Use connection pooling

Advanced Configuration

Custom JVM Options

Configure JVM options:

# In start command
java -Xmx512m -Xms256m -jar target/*.jar

Multi-Profile Configuration

Use different profiles:

# application.properties (default)
spring.profiles.active=${SPRING_PROFILES_ACTIVE:default}

# application-production.properties
spring.datasource.url=${DATABASE_URL}
spring.jpa.show-sql=false

# application-development.properties
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.show-sql=true

Docker Support

For local development with Docker:

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Examples

Basic Spring Boot App

// src/main/java/com/example/Application.java
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
// src/main/java/com/example/controller/HelloController.java
package com.example.controller;

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

@RestController
public class HelloController {
@GetMapping("/")
public String hello() {
return "Hello, Deployxa!";
}
}

REST API

// src/main/java/com/example/model/User.java
package com.example.model;

import jakarta.persistence.*;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;
private String email;

// Getters and setters
}
// src/main/java/com/example/repository/UserRepository.java
package com.example.repository;

import com.example.model.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
// src/main/java/com/example/controller/UserController.java
package com.example.controller;

import com.example.model.User;
import com.example.repository.UserRepository;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserRepository userRepository;

public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}

@GetMapping
public Iterable<User> getAllUsers() {
return userRepository.findAll();
}

@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}

With Database Migration

-- src/main/resources/db/migration/V1__Create_users_table.sql
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Resources


Need Help? Contact support@deployxa.com or join our community Discord.