๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒฟ Spring

[Spring Boot/RESTful] Swagger ์„ค์ •(+๋ฒ„์ „ ์˜ค๋ฅ˜)

by nitronium102 2022. 2. 18.

Swagger

API ์„ค๊ณ„, ๋นŒ๋“œ, ๋ฌธ์„œํ™”, ์‚ฌ์šฉ์— ๋„์›€์„ ์ฃผ๋Š” ์˜คํ”ˆ์†Œ์Šค ํ”„๋ ˆ์ž„์›Œํฌ

 

Swagger ๊ตฌํ˜„

1. 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>

๋ฒ„์ „ ์˜ค๋ฅ˜

spring boot 2.6 ๋ฒ„์ „ ์ดํ›„์— spring.mvc.pathmatch.matching-strategy ๊ฐ’์ด ant_path_matcher์—์„œ path_pattern_parser๋กœ ๋ณ€๊ฒฝ๋˜๋ฉด์„œ ๋ช‡๋ช‡ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(swaggerํฌํ•จ)์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Failed to start bean 'documentationPluginsBootstrapper'; 
nested exception is java.lang.NullPointerException: 
Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" 
because "this.condition" is null

์•„๋ž˜ ๊ฐ’์„ application.yml์— ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•จ.

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

⇒ ํ•˜์ง€๋งŒ ์–˜๋Š” ๋’ค์—์„œ ๋‹ค๋ฃฐ actuator๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค(actuator๋Š” pathpattern-based parsing์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ)

 

2. Config ํŒŒ์ผ ์„ค์ •

localhost:8088/v2/api-docs

localhost:8088/swagger-ui/index.html/ ๋‘ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ณดํ†ต์€ ์•„๋ž˜ ๊ฑธ ๋งŽ์ด ์“ด๋‹ค.

@Configuration // ์„ค์ • ๊ด€๋ จ annotation
@EnableSwagger2 // SWAGGER ๊ด€๋ จ
public class SwaggerConfig {
	// localhost:8088/v2/api-docs
	// localhost:8088/swagger-ui/index.html/

	// Swagger Documentation
	private static final Contact DEFAULT_CONTACT = new Contact("Minji Kang",
		"https://github.com/test1234", "test1234@gmail.com");

	private static final ApiInfo DEFAULT_API_INFO = new ApiInfo("API TITLE",
		"My User Management REST API Service", "1.0", "urn:tos", DEFAULT_CONTACT,
		"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<>());

	// asList : ๋ฐฐ์—ด ํ˜•ํƒœ -> ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ
	private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES = new HashSet<>(
		Arrays.asList("application/json", "application/xml")); // json๊ณผ xml ํŒŒ์ผ ํ˜•์‹์„ ์ง€์›ํ•จ

	@Bean
	public Docket api(){
		return new Docket(DocumentationType.SWAGGER_2)
			.apiInfo(DEFAULT_API_INFO)
			.produces(DEFAULT_PRODUCES_AND_CONSUMES)
			.consumes(DEFAULT_PRODUCES_AND_CONSUMES);
	}

3. domain ์„ค์ •

swagger์— ๋‚˜ํƒ€๋‚ผ ์„ค๋ช…์„ @ApiModel, @ApiModelProperty๋ฅผ ์ด์šฉํ•ด ์ž‘์„ฑํ•œ๋‹ค.

@Data
@AllArgsConstructor
@NoArgsConstructor // ๋””ํดํŠธ ์ƒ์„ฑ์ž ์ƒ์„ฑ
@ApiModel(description = "์‚ฌ์šฉ์ž ์ƒ์„ธ ์ •๋ณด๋ฅผ ์œ„ํ•œ ๋„๋ฉ”์ธ ๊ฐ์ฒด")
public class User {
	private Integer id;

	@Size(min=2, message = "Name์€ 2๊ธ€์ž ์ด์ƒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
	@ApiModelProperty(notes = "์‚ฌ์šฉ์ž ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
	private String name;

	@Past // ๊ณผ๊ฑฐ ๋‚ ์งœ๋งŒ ๊ฐ€๋Šฅํ•œ ์ œ์•ฝ ์กฐ๊ฑด
	@ApiModelProperty(notes = "์‚ฌ์šฉ์ž์˜ ๋“ฑ๋ก์ผ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
	private Date joinDate;

	@ApiModelProperty(notes = "์‚ฌ์šฉ์ž์˜ ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
	private String password;

	@ApiModelProperty(notes = "์‚ฌ์šฉ์ž์˜ ์ฃผ๋ฏผ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
	private String ssn; // ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ
}

 

๋‚˜์ค‘์— ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์€ ์งˆ๋ฌธ

https://www.inflearn.com/questions/224450

 

Swagger์™€ Jackson Filter ์‚ฌ์šฉ ์‹œ Swagger-ui์˜ example value - ์ธํ”„๋Ÿฐ | ์งˆ๋ฌธ & ๋‹ต๋ณ€

Jackson Filter๋ฅผ ์ ์šฉํ•œ ์ฝ”๋“œ์—์„œ๋Š” Swagger-UI์—์„œ example value ๊ฐ’์ด 'filters'์™€ 'value'๋กœ๋งŒ ๋ณด์ž…๋‹ˆ๋‹ค. ํ•„ํ„ฐ๊ฐ€ ์ ์šฉ๋œ example value ๊ฐ’์„ ์˜จ์ „ํžˆ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค. [์‚ฌ์ง„] [์‚ฌ

www.inflearn.com

 

๋Œ“๊ธ€