๐ํ๊ฒฝ
IntelliJ Ultimate
Java 17
Spring boot 3.2.3
Gradle - Groovy
Dependencies:
Spring Web
Thymeleaf
Spring Data JPA
lombok
MariaDB 10.11
Spring Dev tool
๐๋ฌธ์
Swagger Configuration ์์ฑ ์ ๊ธฐ์กด์ ์ฌ์ฉํ๋ ์ ์ ๋ฆฌ์์ค ๋ด ๋ชจ๋ ํ์ผ์ด ์ธ์๋์ง ๋ชปํจ(No mapping GET...), swagger-ui/index.html๊ฐ 404๋ก ๋จ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฌ ๊ธ์ ์์ฑํ๋ค.
dependencies {
...
// swagger
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
}
์์กด์ฑ ์ถ๊ฐ ์์๋ ์๋ฌด๋ฐ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ง ์๋๋ค. ๋ค๋ง localhost:8080/swagger-ui/index.html๋ก ์ ์ํ๋ฉด 404๊ฐ ๋ด๋ค.
์ฌ๊ธฐ์ `SwaggerConfig`์ `CustomServletConfig`๋ฅผ ์์ฑํ๋ฉด ํด๋น ํ์ด์ง์ ์ ์ ๋ฌธ์ ๋ ํด๊ฒฐ๋๋ค.
import springfox.documentation.api.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.OAS_30)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.basePackage("org.zerock.b01.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Boot 01 Project Swagger")
.build();
}
package org.zerock.b02.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
@EnableWebMvc
public class CustomServletConfig extends WebMvcConfigurationSupport {
}
๋๋น! ํ๊ณ ์ ๋๊ฒ ๋์์๋ ์๊ฐ ๋ค๋ฅธ ํ์ด์ง์์ ๊ธฐ์กด์ ์ ์ฉํ๋ CSS๊ฐ ๋ชจ๋ ๋ฐ์ด๋๋ ๊ฒ์ ๋ชฉ๊ฒฉํ ์ ์๋ค.
Swagger UI๊ฐ ์ ์ฉ๋๋ฉด์ ์ ์ ํ์ผ์ ๊ฒฝ๋ก๊ฐ ๋ฌ๋ผ์ก๊ธฐ ๋๋ฌธ์ด๋ค.
์์นํ ๋๋ก `@EnableWebMVC`๋ฅผ ์ง์ฐ๋ฉด swagger-ui/index.html์ด ์ ์๋์ง ์๊ณ , ๋ฃ์ผ๋ฉด CSS๊ฐ ๋ฐ์ด๋ฌ๋ค. ๋์ด ์ฌ์ด์ข๊ฒ ์ง๋์ผ๋ฉด ์ข๊ฒ๋๋
์ฐพ์๋ณด๋ `build.gradle`์ ์์กด์ฑ์ผ๋ก ์ถ๊ฐํ springfox๊ฐ ์คํ๋ง ๋ถํธ 3.0 ์ด์์ ์ฌ์ฉํ ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค๋ ์ฌ์ค์ ๋ฐ๊ฒฌํ๋ค.
๐ํด๊ฒฐ
๋ด ๋ฌธ์ ๊ฐ ์๋๋ผ ์คํ๋ง ๋ถํธ + springfox์ ์ถฉ๋ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ผ๋ springfox์์ springdoc์ผ๋ก ์ข ์์ฑ์ ๋ณ๊ฒฝํด์ค๋ค.
springfox์ springdoc์ ๋ ๋ค Swagger๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค. springfox๋ 2020๋ ์ดํ๋ก ์ ๋ฐ์ดํธ๋ฅผ ์ค๋จํ๊ณ , springdoc์ 2019๋ ์ ์ฒ์ ๋์๋ค. ๊ธ์ ์์ฑํ๋ ํ์ฌ 2024๋ 3์ ์์ 3๊ฐ์ ์ ์ธ 2023๋ 12์์๋ ์ต๊ทผ ์ ๋ฐ์ดํธ ๋์๋ค.
dependencies {
...
// swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
}
springdoc์ ๋ง์ถ์ด `SwaggerConfig`๋ฅผ ๋ณ๊ฒฝํ๊ณ ,`CustomServletConfig`๋ ์ญ์ ํด์ค๋ค.
package org.zerock.b02.config;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI openAPI() {
return new OpenAPI()
.components(new Components())
.info(apiInfo());
}
private Info apiInfo() {
return new Info()
.title("Springdoc ํ
์คํธ")
.description("Springdoc์ ์ฌ์ฉํ Swagger UI ํ
์คํธ")
.version("2.2.0");
}
}
swagger-ui/index.html๋ ์ ๋์ค๊ณ , ์ ์ ๋ฆฌ์์ค ํ์ผ๋ ๋ชจ๋ ์ ๋งคํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค!
๐ springdoc์ ์ด๋ป๊ฒ ์จ์
[Spring] ์คํ๋ง ๋ถํธ 3.2.3์์ Swagger(springdoc) ์ฌ์ฉํ๊ธฐ!
๐์ฐธ๊ณ
[Springdoc] No mapping for GET /swagger-ui/index.html ์ค๋ฅ ์กฐ์น
์ง๋๋ฒ์ Springdoc ์ฌ์ฉ๋ฒ ์ด๋๊ฐ์ง๊ณ ๊ธ๋ ์ฐ๊ณ ์ด๋ ๊ฒ ์ฐ๋ฉด ์ ๋๋ค๊ณ ํ์๋ค. ์ง๊ธ๋ ํ๋ก์ ํธ ์๋ก ๋ง๋ค์ด์ ์ ์ ์ผ๋ ๊ธ๋๋ก ์งํ์ ํ๋ฉด ์ ๋๋ค. ํ์ง๋ง ์ด๋ฏธ ๊ฐ๋ฐ์ด ์๋ฃ๋ ์ด๋ค ํ๋ก์
oingdaddy.tistory.com
'Java > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] Mac M1 ์คํ๋ง๋ถํธ ์คํ ์ Port 8080 is already in use (0) | 2024.03.13 |
---|---|
[SpringBoot] ์คํ๋ง ๋ถํธ 3.2.3์์ Swagger(springdoc) ์ฌ์ฉํ๊ธฐ! (0) | 2024.03.13 |
[SpringBoot/JPA] ๊ฒ์์ ํฌํจํ ํ์ด์ง ์ฒ๋ฆฌ DTO (0) | 2024.03.13 |
[SpringBoot/JPA] ModelMapper๋ก Entity์ DTO ๋ณํํ๊ธฐ (0) | 2024.03.13 |
[SpringBoot/JPA] Querydsl์ ์ฌ์ฉํ ๋์ ์ฟผ๋ฆฌ (0) | 2024.03.12 |