μν κ΄λ¦¬ REST API μλ² κ΅¬ν
1.
μν ν
μ΄λΈ μ μ
2.
Spring Boot REST API ꡬν
3.
API λͺ
μΈμ μλ μμ± - OpenAPI(Swagger)
4.
API ν
μ€νΈ
μν ν μ΄λΈ μ μ
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`no` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'PK',
`id` VARCHAR(64) NOT NULL COMMENT 'UK',
`title` VARCHAR(100) NOT NULL COMMENT 'μνλͺ
',
`content` TEXT NULL COMMENT 'μ€λͺ
',
`likes` BIGINT NULL DEFAULT 0 COMMENT 'μ’μμ',
`img` TEXT NULL COMMENT 'μ΄λ―Έμ§κ²½λ‘',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'λ±λ‘μΌμ',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'μμ μΌμ'
);
SQL
볡μ¬
Spring Boot REST API ꡬν
μμ² λ©μλ | μμ² κ²½λ‘ | νλͺ© | λΉκ³ |
GET | /products | μν λͺ©λ‘ | |
GET | /products/{id} | μν μ‘°ν | |
POST | /products | μν λ±λ‘ | |
PUT | /products | μν μμ | |
DELETE | /products/{id} | μν μμ |
β’
domain
β¦
Product.java
β’
Mapper
β¦
ProductMapper.xml
β¦
BaseMapper.java
β¦
ProductMapper.java
β’
Service
β¦
BaseService.java
β¦
ProductService.java
β¦
ProductServiceImpl.java
β’
API λͺ μΈμ μλ μμ± - OpenAPI(Swagger)
// Springdoc openapi
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'
SQL
볡μ¬
build.gradle
plugins {
id 'java'
id 'war'
id 'org.springframework.boot' version '3.4.1'
id 'io.spring.dependency-management' version '1.1.6'
}
group = 'com.aloha'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter-test:3.0.4'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
// Springdoc openapi
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0'
}
tasks.named('test') {
useJUnitPlatform()
}
SQL
볡μ¬
SwaggerConfig.java
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
@Configuration
public class SwaggerConfig {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("aloha") // κ·Έλ£Ήλͺ
μ€μ
.pathsToMatch("/**") // κ²½λ‘ μ€μ
.build();
}
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("μν κ΄λ¦¬ Proejct API")
.description("μν κ΄λ¦¬ νλ‘μ νΈ API μ
λλ€.")
.version("v0.0.1"));
}
}
Java
볡μ¬