Skip to content

TD99/spring-boot-openapi-generator-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAPI Generator Example

Java Tests Angular Build Open Issues Closed Issues

Spring Angular Apache Maven openapi initiative

This project demonstrates how to use OpenAPI Generator with Spring Boot 3, to build a simple REST API for managing TODOs.
It uses the delegate pattern to separate generated code from custom business logic.

Features

  • OpenAPI 3.0 specification-driven development
  • Automatic Spring Boot controller & DTO generation
  • Delegate pattern for business logic separation
  • JPA & H2 in-memory database integration
  • Swagger UI for API exploration
  • Unit and integration tests with JUnit 5
  • Minimal frontend for showcasing API usage

Getting Started (Backend)

The project comes with a Spring Boot backend to showcase the usage of the OpenAPI Generator. It is located in the root and src directory.

Prerequisites

  • Java 21+
  • Maven 3.9+

Build & Run

Run the application:

# Generate code from OpenAPI spec and build the application
mvn clean compile

# Run the application
mvn spring-boot:run

Base URL

The application is available at http://localhost:8080.

API Endpoints

Method Path Description
GET /api/todos List all todos
POST /api/todos Create a todo
GET /api/todos/{id} Get a todo by ID
PUT /api/todos/{id} Update a todo
PATCH /api/todos/{id} Patch a todo
DELETE /api/todos/{id} Delete a todo

Management Endpoints

Name Path Description Credentials
Swagger UI /swagger-ui API testing UI None
OpenAPI Spec /v3/api-docs OpenAPI specification None
H2 Console /h2-console In-memory database UI Username: sa
Password: <empty>

Getting Started (Frontend)

The project comes with a minimal Angular frontend to showcase the API usage. It is located in the frontend directory.

Development Mode

If you are actively developing the frontend, you can run it alongside the Spring Boot backend. This setup supports hot reloading when modifying Angular code.

Prerequisites

  • Frontend
    • Node.js 20+
    • Angular CLI 20+ (optional)
  • Backend (if needed)
    • Java 21+
    • Maven 3.9+

Install Dependencies

# Install frontend dependencies
cd frontend
npm install

Build & Run

Run backend and frontend separately:

# Generate code from OpenAPI spec and build the application
mvn clean compile

# Run the backend
mvn spring-boot:run
# In another terminal, run the frontend development server
npm --prefix ./frontend start

Endpoints

Type Mode URL Notes
Frontend Direct http://localhost:4200 Hot reload enabled
Backend (API) Direct http://localhost:8080/api URL root is reserved for Spring Boot Mode
Backend (API) Proxy http://localhost:4200/api Proxy via proxy.conf.json

The Angular dev server is configured with a proxy to forward /api requests to the backend.

Spring Boot Mode

If the frontend is not actively being developed, it will be served directly by the Spring Boot backend. This setup does not support hot reloading when modifying Angular code.

Prerequisites

  • Java 21+
  • Maven 3.9+

Build & Run

Run backend and frontend together:

# Build the application
mvn clean compile

# Run the application
mvn spring-boot:run

Endpoints

Type Mode URL Notes
Frontend Direct http://localhost:8080 Hot reload disabled
Backend (API) Direct http://localhost:8080/api URL is the same as in Development Mode

Technical Notes

  • Spring Boot Mode is always active when running the application (even if the frontend is in Development Mode).
  • It uses the frontend-maven-plugin to install Node.js, run Angular builds, and integrate the frontend with the backend.
  • During the Maven build (mvn clean compile), the Angular app is compiled (npm run build) and copied into Spring Boot’s static/ resources directory:
    frontend/dist/openapi-generator-example-frontend/browsertarget/classes/static
  • This allows the packaged Spring Boot application to serve the frontend directly without needing a separate Node.js process in production mode.

License

This project is licensed under the MIT License.

About

Spring Boot 3 project demonstrating OpenAPI Generator with delegate pattern

Topics

Resources

License

Stars

Watchers

Forks