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
'๐ฟ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot/RESTful] HAL Browser(Explorer) (+๋ฒ์ ์๋ฌ) (0) | 2022.02.18 |
---|---|
[Spring Boot/RESTful] REST API Monitoring์ ์ํ Actuator(+spring fox PathPatternParser ์ด์) (0) | 2022.02.18 |
[Spring Boot/RESTful] REST API 3๋จ๊ณ๋ฅผ ์ํ HATEOAS ์ค์ (0) | 2022.02.18 |
[Spring boot/RESTful] REST API ๋ฒ์ ๊ด๋ฆฌ (0) | 2022.02.18 |
[Spring boot/RESTful] Response Filtering (0) | 2022.02.18 |
๋๊ธ