Swagger는 REST API를 설계, 빌드, 문서화 및 사용하는 데 도움이 될 수 있는 OpenAPI 사양을 기반으로 구축된 오픈 소스 도구 세트이며, 이 swagger를 프로젝트에 적용 방법을 설명한다.
dependencies { compile 'io.springfox:springfox-swagger2:2.9.2' compile 'io.springfox:springfox-swagger-ui:2.9.2' }
package com.gsc.process.integration.config; 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; @Configuration @EnableSwagger2 public class SwaggerConfiguration { private ApiInfo swaggerInfo() { return new ApiInfoBuilder() .title("GSC Process Integration - Swagger Examples") .description("GSC Process Integration Sample API Document") .version("0.0.1") .build(); } @Bean public Docket swaggerApi() { return new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .select() .apis(RequestHandlerSelectors.basePackage("com.gsc.process.integration")) .paths(PathSelectors.ant("/api/**")) //.paths(PathSelectors.ant("/**")) .build() .apiInfo(swaggerInfo()); } }
package com.gsc.process.integration.sample; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; @SuperBuilder @Data @EqualsAndHashCode(callSuper = false) @NoArgsConstructor @AllArgsConstructor @ApiModel(description = "공통 코드 모델") public class SampleApiModel { @ApiModelProperty("그룹 코드") private String grpCd; @ApiModelProperty("상세 코드") private String dtlCd; @ApiModelProperty("코드명") private String cdNm; @Data public static class Criteria { @ApiModelProperty("사용여부") private String useYn; } }
package com.gsc.process.integration.sample; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @Api("OpenAPI for GSC Process Integration Project") @RestController public class SampleApiController { @Autowired SampleApiService sampleApiService; @GetMapping("/api/sample/return-map-api") @ApiOperation(value = "Map 반환 API", notes = "Map을 반환하는 API") public Map<String, Object> returnMapApi() { Map<String, Object> map = new HashMap<>(); map.put("test1", 1); map.put("test2", 2); return map; } @GetMapping("/api/sample/cmm-code") @ApiOperation(value = "공통 코드 반환", notes = "공통 코드 전체 리스트 반환") public List<SampleApiModel> getAllCommonCode(@ApiParam(value = "사용 여부", required = false, example = "Y") @RequestParam String useYn) { return sampleApiService.getAll(null); } }