dev. environment.
SpringBoot 3.1.1
Java 17
μ€μ¨κ±°(Swagger) λ?
- κ°λ°μκ° REST API μλΉμ€λ₯Ό μ€κ³, λΉλ, λ¬Έμνν μ μλλ‘ νλ νλ‘μ νΈ
- REST APIλ₯Ό λ¬Έμννλ λꡬμ΄λ©°, APIμ λν λͺ μΈ(Spec)μ κ΄λ¦¬νκΈ° μν νλ‘μ νΈ
- APIκ° μμ λλλΌλ λ¬Έμκ° μλμΌλ‘ κ°±μ .
- 2011 λ μ μ²μ λμ λ κ°λ
κΈ°μ‘΄, μλλ μμ λ± μκΈ° μμ±νλ λ¬Έμν API λͺ μΈμμ λ¬λ¦¬ μλ²μ μΌμ μ ν μ ν΄μ£Όλ©΄ API λͺ μΈμκ° κ°±μ λλ€.
λ°±μλ κ°λ°μμ κ²½μ° ν΄λΌμ΄μΈνΈλ₯Ό κ°λ°νλ νλ‘ νΈμλ κ°λ°μμ μν΅μ νλ €λ©΄ API λͺ μΈμκ° κΌ νμνκ² λλλ°
μ΄κ±Έ νλνλ λͺ μΈνκ³ μλ κ²λ κ½€λ ν° μΌλ‘ νμ λλ€.
μ΄λ° μ 무 λ‘μ€λ₯Ό μ€μ΄κΈ° μν΄ μλ² λ¨μ μ μ©νλ κ²μ΄ λ°λ‘λ°λ‘ μ€μ¨κ±°λ€.
μ€μ¨κ±°(Swagger)μ μ’ λ₯
Spring-Fox
- 2015 λ μ λμ¨ λΌμ΄λΈλ¬λ¦¬
- 2020λ 7μ 3.0.0 λ²μ μ λ§μ§λ§μΌλ‘ μ λ°μ΄νΈκ° λ©μΆ€
Spring-Dox
- 2019λ 7μμ λμ¨ λΌμ΄λΈλ¬λ¦¬ - SpringFoxκ° μ λ°μ΄νΈλ₯Ό μ€λ¨νλ μμ ..
- 2023λ 4μκΉμ§ μ λ°μ΄νΈκ° μ§νλκ³ μλ€. (23.10 κΈ°μ€)
- webfluxλΌλ λ Ό λΈλ‘νΉ λΉλκΈ° λ°©μμ μΉ κ°λ°μ μ§μνλλ‘ κ°λ°λ¨
κ³ λ‘ μ΅μ λ²μ μ springboot μ κ²½μ° spring-docμ μ¬μ©νλ κ²μ΄ μ©μ΄νλ€
SpringDocμ μ¬μ©ν Swagger μ μ©
1. λΌμ΄λΈλ¬λ¦¬ μΆκ°
swagger κ΄λ ¨ λΌμ΄λΈλ¬λ¦¬μ κ²½μ° λ²μ νμΈμ΄ κΌ νμνλ€,
2.x.x λ²μ κ³Ό 3.0.0λ²μ μ΄ swagger uiλ₯Ό λΆλ¬μ¬ λ μ¬μ©νλ URLμ΄ λ€λ₯΄κΈ° λλ¬Έ.
....
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
....
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
.....
2. μ½λ μμ±
SwaggerConfig.java νμΌκ³Ό TestController.java νμΌμ μμ±ν΄λ³΄μ.
SwaggerConfig.java
package com.example.swagger.demo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* The Class SwaggerConfig.
*/
@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() {
String description = "Welcome Log Company";
return new ApiInfoBuilder()
.title("SWAGGER TEST")
.description(description)
.version("1.0")
.build();
}
}
TestController.java
package com.example.swagger.demo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* The Class TestController
*/
@RestController
public class TestController {
@GetMapping(value = "/hello")
@ApiOperation(value = "hello, world api", notes = "hellow world swagger check")
public String hellowWorld(){
return "hello, world";
}
@ApiOperation(value = "test", notes = "ν
μ€νΈμ
λλ€")
@ApiResponses({
@ApiResponse(code = 200, message = "ok"),
@ApiResponse(code = 404, message = "page not found!")
})
@GetMapping(value = "/board")
public Map<String, String> selectBoard(@ApiParam(value = "μνλ²νΈ", required = true, example = "1")
@RequestParam String no) {
Map<String, String> result = new HashMap<>();
result.put("test title", "ν
μ€νΈ");
result.put("test contents", "ν
μ€νΈ λ΄μ©");
return result;
}
}
3. Swagger UI νμΈ
Swagger λ²μ μ λ°λ₯Έ URL
2.x.x : http://localhost:8080/swagger-ui.html
2.x.x : http://localhost:8080/swagger-ui/index.html
ν΄λΉ URLμ λ€μ΄κ°κ² λλ©΄.. μλμ κ°μ΄ ν μ€νΈ νλ©΄μ΄ λνλκ² λλ€.