HyunsooZo's TIL logo HyunsooZo's TIL

Creating Api doc using Swagger

1. Add the following dependencies to build.gradle file:

	implementation 'io.springfox:springfox-boot-starter:3.0.0'
	implementation 'io.springfox:springfox-swagger-ui:3.0.0'

2. Add the following configuration to the application.properties file:

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

3. create a Class - [SwaggerConfig]

@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());
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("title")
                .description("description")
                .version("1.0")
                .build();
    }
}
    // api설명 
    @ApiOperation(value = "선택한 기간 내 모든 일기 데이터 불러오기", notes = "note..") 
    @GetMapping("/read/diaries")
    List<Diary> readDiaries(
            @RequestParam @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
            //api parameter 예시나 설명 등.. 
            @ApiParam(value="조회할 기간의 첫번째 날",example = "2020-02-02")LocalDate startDate,

            @RequestParam @DateTimeFormat(iso=DateTimeFormat.ISO.DATE)
            @ApiParam(value="조회할 기간의 마지막 날",example = "2020-02-02") LocalDate endDate
    ){
        return diaryService.readDiaries(startDate,endDate);
    }
TOP