SPRING
2018.06.18 / 10:29

Spring REST API¿¡ Swagger(½º¿þ°Å)¼³Á¤Çϱâ

¸®¾ó¸®
Ãßõ ¼ö 215

Swagger(½º¿þ°Å)´Â API¿¡ ´ëÇÑ ¸Þ´º¾ó ÀÚµ¿ »ý¼º ¹× Å×½ºÆ® ±â´ÉÀ» Á¦°øÇÕ´Ï´Ù. 




¸ÕÀú ½º¿þ°Å2 ±¸ÇöÀ» À§ÇÑ Springfox»ç¿ëÀ» À§ÇØ pom.xml¿¡ dependency¸¦ Ãß°¡ ÇÕ´Ï´Ù.

½º¿þ°Å UI´Â ½º¿þ°Å°¡ »ý¼ºÇÏ´Â API¹®¼­¸¦ »ç¿ëÀÚ ´ëÈ­ ¹æ½ÄÀ¸·Î ¸¸µé¾î ÁÖ´Â ³»Àå ¼Ö·ç¼ÇÀÔ´Ï´Ù.


<!--Swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<!--Swagger UI-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>


½º¿þ°Å2´Â @EnableSwagger2 ¾î³ëÅ×ÀÌ¼Ç ¼³Á¤À¸·Î »ç¿ë °¡´ÉÇÕ´Ï´Ù. 

@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())
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET,
newArrayList(new ResponseMessageBuilder()
.code(500)
.message("500 message")
.responseModel(new ModelRef("Error"))
.build(),
new ResponseMessageBuilder()
.code(403)
.message("Forbidden!")
.build()));
}
private ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo(
"API Title ",
"API description.",
"API 1.0",
"Terms of service",
"annajinee@gmail.com",
"License of API",
"API license URL");
return apiInfo;
}
}



Àüü ¼Ò½º´Â ¾Æ·¡ÀÇ ±ê¿¡¼­ È®ÀÎ °¡´ÉÇÕ´Ï´Ù. 

https://github.com/annajinee/springboot-swagger-api


Âü°í »çÀÌÆ® :  http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api

       http://springboot.tistory.com/26



Ãâó: http://annajinee.tistory.com/26?category=762989 [Hello, world!]