Skip to content

Latest commit

 

History

History
150 lines (122 loc) · 5.32 KB

README.md

File metadata and controls

150 lines (122 loc) · 5.32 KB

Spring Boot Boilerplate

Spring Boot Logo

Spring Boot Boilerplate is a starter kit designed to help you quickly get started with building a Spring Boot application. This project provides a simple and useful template that incorporates various essential technologies and configurations.

Technologies Used

  • Spring Boot (v2.7.10): Simplifies the development of Spring applications.
  • Spring Data JPA: Provides easy integration with relational databases using JPA.
  • Spring Validation: Handles validation of Java Beans.
  • Spring Security + JWT Token: Secures your application with JWT-based authentication.
  • PostgreSQL: A powerful, open source object-relational database system.
  • MapStruct: A code generator that simplifies the mapping of Java Beans.
  • Lombok: Reduces boilerplate code for model objects by generating getters, setters, and other methods.
  • Swagger (Open API): Provides interactive API documentation.

Project Structure

The project follows a standard Maven project layout:

spring-boot-boilerplate
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com/gdsc/boilerplate/springboot
│   │   │       ├── configuration
│   │   │       ├── controller
│   │   │       ├── dto
│   │   │       ├── exception
│   │   │       ├── mapper
│   │   │       ├── model
│   │   │       ├── repository
│   │   │       ├── security
│   │   │       ├── service
│   │   │       ├── utils
│   │   │       └── SpringBootBoilerplateApplication.java
│   │   └── resources
│   │       ├── application.yml
│   │       └── static
│   └── test
│       └── java
│           └── com/gdsc/boilerplate/springboot
└── pom.xml

Customization

Token Information

You can customize the JWT token information such as secret key, issuer, and expiry date in the application.yml file.

jwt:
  secret: your_secret_key
  issuer: your_issuer
  expiry: 86400000 # in milliseconds

Database Connection

You can customize the database connection information in the application.yml file.

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/your_db
    username: your_username
    password: your_password
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Swagger Configuration

You can customize Swagger settings in the application.yml file.

swagger:
  enabled: true
  title: Spring Boot Boilerplate API
  description: API documentation for the Spring Boot Boilerplate project
  version: 1.0.0
  contact:
    name: Your Name
    url: http://your-url.com
    email: [email protected]

Security Configuration

You can customize which endpoints are accessible without token information in the SecurityConfiguration.java file.

	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
	
		return http.cors().and().csrf(csrf -> csrf.disable())
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .authorizeRequests(requests -> requests
                        .antMatchers("/register", "/login", "/v3/api-docs/**", "/swagger-ui/**", "/api-docs", "/actuator/**").permitAll()
                        .anyRequest().authenticated())
                .exceptionHandling(handling -> handling.authenticationEntryPoint(unauthorizedHandler))
                .sessionManagement(management -> management.sessionCreationPolicy(SessionCreationPolicy.STATELESS)).build();

	}

Running the Application

Prerequisites

  • Docker: Make sure Docker is installed and running.
  • Java: Ensure you have Java 11 or higher installed.
  • Maven: Make sure Maven is installed.

Deploy with docker

If you're using Docker, you can use the following command to start the containers in detached mode:

docker-compose up -d

Build the Project

Navigate to the root of the project and run the following command to build the project:

mvn clean install

Run the Application

Navigate to the target directory and run the application:

java -jar target/spring-boot-boilerplate.jar

Using Swagger

Once the application is running, you can access the Swagger UI at:

http://localhost:8080/api-docs

Design API

https://gdsc-boilerplate-swagger-docs-api-latest.onrender.com/

This provides interactive documentation for your API endpoints.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.