SpringBoot

SpringBoot - Swagger2, MySQL, JPA 설치 및 셋팅 (maven)

hminor 2024. 2. 19. 17:38

Swagger2

- pom.xml

<!-- Swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

- config/SwaggerConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}

커스텀이 필요하다면 아래와 같이 작성

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo())
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "Sample REST API",
                "This is sample API.",
                "V1",
                "Terms of service",
                new Contact("administrator", "www.example.com", "administrator@email.com"),
                "License of API", "www.example.com", Collections.emptyList());
    }

}

이후 http://localhost:8080/swagger-ui.html <- 해당 경로로 들어가게 되면 확인할 수 있다.

만약 에러가 발생한다면

- resources 폴더에 application.properties나 application.yml 파일을 생성 후 코드 추가하기
  (작성자는 yml로 작성하기에 properties는 따로 변환이 필요)

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 


MySQL

- 프로그램 설치
https://dev.mysql.com/downloads/installer/

 

MySQL :: Download MySQL Installer

Note: MySQL 8.0 is the final series with MySQL Installer. As of MySQL 8.1, use a MySQL product's MSI or Zip archive for installation. MySQL Server 8.1 and higher also bundle MySQL Configurator, a tool that helps configure MySQL Server.

dev.mysql.com

- pom.xml

우선 mysql을 사용할 것이기에 기존 셋팅되어있는 hsqldb는 제거하기

// hsqldb 제거
<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.5.2</version>
</dependency>

mysql 셋팅

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

- application.yml

// 개행 꼭 잘 맞춰야 함.

spring:
    datasource:
        url: jdbc:mysql://localhost:3306/DB명작성?serverTimezone=Asia/Seoul&zeroDateTimeBehavior=convertToNull
        username: 본인 환경의 DB 유저명
        password: 본인 환경의 DB 유저의 비밀번호
        driver-class-name: com.mysql.cj.jdbc.Driver

    jpa:
        database: mysql
        show-sql: true
        database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
        hibernate:
          ddl-auto: update

 


JPA

- pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
</dependency>

- application.yml

server:
  address: localhost
  port: 8080

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

  datasource:
    url: jdbc:mysql://localhost:3306/back?serverTimezone=Asia/Seoul&zeroDateTimeBehavior=convertToNull
    username: 본인 환경의 DB 유저명
    password: 본인 환경의 DB 유저의 비밀번호
    driver-class-name: com.mysql.cj.jdbc.Driver

  jpa:
    database: mysql
    show-sql: true
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    hibernate:
      ddl-auto: update