Restful API를 만들 때 문서화를 만드는것이 중요하다.
그래서 이번에는 Swagger 2 를 사용하여 Restful API 문서화를 어떻게 구성하고 사용하지를 설명하겠다.
■ 의존성 추가
☞ Gradle
//swagger2
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.5.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.5.0'
☞ Maven
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
■ Sprinboot프로젝트에 구성하기
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
■ swagger사용
설명 @ApiOperation(value = "서버로 부터 FloatingIP 삭제") @ApiImplicitParams({ @ApiImplicitParam( paramType = "body", name = "body", dataType="java.util.Map", examples = @Example(value = { @ExampleProperty(mediaType="application/json", value="{\"address\":\"\",\"serverId\":\"\"}")})) }); |
@ApiOperation(value = "test")
@POST
@Path("external/info")
public void postTest(@ApiParam(value = "test",
examples = @Example(value = {
@ExampleProperty(mediaType="application/json", value="[\"a\",\"b\"]")
})) ArrayList<String> tenantId) {
return;
}
@ApiOperation(value = "Server 리스트 조회 ")
@RequestMapping(value = {"/servers"}, method = RequestMethod.GET)
public ResponseEntity<List<ResServers>> getServers(HttpServletRequest request) throws Exception {
//new Openstack4j API
Openstack4jClient client = getOpenstack4jClient();
return serverService.getServers(client);
}
■ swagger 접속 확인
http://URL/swagger-ui.htm |