Development of an API using SpringBoot + Kotlin with the aim of registering Avengers.
- Intellij 2023.3.7
- SpringBoot 3.3.3
- Maven
- Kotlin
- SpringData JPA
- PostgreSQL
- Flyway
- Java 17
- Heroku
-
Resource
avenger
-
GET - 200 OK
-
GET {id}/detail - 200 OK ou 404 Not Found
-
POST - 201 Created ou 400 Bad Request
-
PUT {id} - 202 Accepted ou 404 Not Found
-
DELETE {id} - 202 Accepted ou 404 Not Found
{
"nick": "spider-man",
"person": "Peter Parker",
"description": "about powers",
"history": "history"
}
- Application Layer (controllers, configs, exception handle, request and response data, bean validations)
- Domain Layer (Avenger model, repository interface, service)
- Infrastructure Layer (JPA repository, Avenger entity, proxy implements repository interface and uses the JPA repository to communicate with the database)
- Tests
create table avenger (
id bigserial not null,
nick varchar(36),
person varchar(128),
description varchar(128),
history text,
primary key (id)
);
alter table avenger add constraint UK_5r88eemotwgru6k0ilqb2ledh unique (nick);
- application.yaml
- application-dev.yaml
- application-heroku.yaml
spring:
application:
name: kotlin-avengers-api
config:
# This configuration allow use profiles as spring 2.3.x version
# In spring 2.4.x version, has changed to:
# spring:
# profiles:
# group:
# <group>: dev, auth
use-legacy-processing: true
profiles:
active: dev
jmx:
enabled: false
data:
jpa:
repositories:
bootstrap-mode: deferred
jpa:
open-in-view: false
properties:
hibernate.jdbc.time_zone: UTC
hibernate.generate_statistics: false
hibernate.jdbc.batch_size: 25
hibernate.order_inserts: true
hibernate.order_updates: true
hibernate.query.fail_on_pagination_over_collection_fetch: true
hibernate.query.in_clause_parameter_padding: true
hibernate:
ddl-auto: none
naming:
physical_naming_strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
main:
allow-bean-definition-overriding: true
task:
execution:
thread-name-prefix: avengers-task-
pool:
core-size: 2
max-size: 50
queue-capacity: 10000
scheduling:
thread-name-prefix: avengers-scheduling-
pool:
size: 2
output:
ansi:
console-available: true
server:
port: 9090
servlet:
session:
cookie:
http-only: true
context-path: /avengers
spring:
# profiles:
# active: dev
jackson:
serialization:
indent-output: true
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://localhost:25433/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
show-sql: true
spring:
profiles:
active: heroku
jackson:
serialization:
indent-output: true
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:postgresql://${HOST}/${DB_NAME}
username: ${DB_USER}
password: ${DB_PASSWORD}
jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
show-sql: false
DB_USER=dio.avenger
DB_PASSWORD=dio.avenger
DB_NAME=avengers
version: '3.2'
services:
postgres:
image: postgres:12-alpine
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_DB: ${DB_NAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
ALLOW_IP_RANGE: 0.0.0.0/0
ports:
- "25433:5432"
volumes:
- pdb12:/var/lib/postgresql/data
networks:
- postgres-compose-network
teste-pgadmin-compose:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: "avengers@email.com"
PGADMIN_DEFAULT_PASSWORD: "123456"
ports:
- "5556:80"
depends_on:
- postgres
networks:
- postgres-compose-network
volumes:
pdb12:
networks:
postgres-compose-network:
driver: bridge
-
docker-compose -f backend-services.yaml up -d
(deploy) /docker-compose -f backend-services.yaml down
(undeploy) -
Start API
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev -Dspring-boot.run.jvmArguments="-Xmx256m -Xms128m" -Dspring-boot.run.arguments="'--DB_USER=avenger' '--DB_PASSWORD=avenger' '--DB_NAME=avengers'"
- Criar app
- Linkar com Github
- Setar variáveis de ambiente
web: java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap $JAVA_OPTS -Dserver.port=$PORT -Dspring.profiles.active=heroku -jar target/*.jar