Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jpa Auditing, Swagger 설정 #27

Merged
merged 5 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions backend/build.gradle
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '17'
sourceCompatibility = '17'
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'

runtimeOnly 'com.h2database:h2'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
useJUnitPlatform()
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootApplication
public class VotogetherApplication {

public static void main(String[] args) {
SpringApplication.run(VotogetherApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(VotogetherApplication.class, args);
}

}
9 changes: 9 additions & 0 deletions backend/src/main/java/com/votogether/config/JpaConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.votogether.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaConfig {
}
40 changes: 40 additions & 0 deletions backend/src/main/java/com/votogether/config/OpenAPIConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.votogether.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.servers.Server;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OpenAPIConfig {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 클래스 명을 OpenAPIConfig라고 하신 이유가 궁금합니다!

Copy link
Collaborator Author

@woo-chang woo-chang Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swagger라는 명칭을 사용하지 않은 이유가 궁금한 것으로 해석했는데 맞을까요?!

저문의 질문 덕분에 저도 Swagger와 OpenAPI의 차이에 대해 알아볼 수 있었습니다 :)

  • OpenAPI : 축약되어 있는 표현으로 조금 더 자세히 기술하면 OpenAPI Spec이라고 합니다. Restful API 디자인에 대한 정의 표준으로 서비스에서 제공하는 API의 기능과 엔드 포인트를 개발자나 시스템이 자동으로 발견하고 처리하는데 필요한 정보를 제공합니다.
  • Swagger : OpenAPI Spec에 맞게 디자인하고 문서화하고 빌드하기 위한 도구들의 모음을 의미합니다.

해당 설정에서는 OpenAPI Spec에 대해 설정하는 부분이기에 OpenAPIConfig라고 정하게 되었습니다!


@Value("${votogether.openapi.dev-url}")
private String devUrl;

@Value("${votogether.openapi.prod-url}")
private String prodUrl;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필드를 final로 지정하지 않은 이유가 있으신가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Value만 단독으로 사용하는 경우 해당 인스턴스가 생성 후 BeanPostProcessor를 통해 프로퍼티가 주입되기에 final을 사용할 수 없었습니다!

해당 값도 final로 설정하면 좋을 것 같아 검색을 통해 찾아보았더니 생성자에서 @Value를 사용하는 경우 생성자가 호출되는 시점에 프로퍼티를 가져오기 때문에 불변으로 설정할 수 있었습니다! 꼼꼼한 리뷰 감사합니다 🙇🏻‍♂️


@Bean
public OpenAPI openAPI() {
final Server devServer = new Server();
devServer.setUrl(devUrl);
devServer.description("개발 환경 서버 URL");

final Server prodServer = new Server();
prodServer.setUrl(prodUrl);
prodServer.description("운영 환경 서버 URL");

final Info info = new Info()
.title("VoTogether API")
.version("v1.0.0")
.description("보투게더 API");

return new OpenAPI()
.info(info)
.servers(List.of(devServer, prodServer));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
public abstract class BaseEntity {

@CreatedDate
@Column(columnDefinition="datetime(2)", updatable = false, nullable = false)
@Column(columnDefinition = "datetime(2)", updatable = false, nullable = false)
private LocalDateTime createdAt;

@LastModifiedDate
@Column(columnDefinition="datetime(2)", nullable = false)
@Column(columnDefinition = "datetime(2)", nullable = false)
private LocalDateTime updatedAt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.votogether.domain.healthcheck.controller;

import com.votogether.domain.healthcheck.service.HealthCheckService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "헬스 체크", description = "헬스 체크 API")
@RequiredArgsConstructor
@RequestMapping("/health-check")
@RestController
public class HealthCheckController {

private final HealthCheckService healthCheckService;

@Operation(summary = "헬스 체크 조회", description = "서버의 작동 여부 확인을 위해 헬스 체크를 조회한다.")
@ApiResponse(responseCode = "200", description = "헬스 체크 조회 성공")
@GetMapping
public ResponseEntity<String> check() {
final String response = healthCheckService.check();
return ResponseEntity.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.votogether.domain.healthcheck.service;

import org.springframework.stereotype.Service;

@Service
public class HealthCheckService {

public String check() {
return "health-check";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Post extends BaseEntity {
@Column(length = 1000, nullable = false)
private String content;

@Column(columnDefinition="datetime(2)", nullable = false)
@Column(columnDefinition = "datetime(2)", nullable = false)
private LocalDateTime deadline;

@Builder
Expand Down
5 changes: 5 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ logging:
descriptor:
sql:
BasicBinder: TRACE

votogether:
openapi:
dev-url: http://localhost:8080
prod-url: http://votogether.com
Copy link
Collaborator

@aiaiaiai1 aiaiaiai1 Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL이 빠져있는것 같아요

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootTest
class VotogetherApplicationTests {

@Test
void contextLoads() {
}
@Test
void contextLoads() {
}

}