❗build.gradle에 swagger 설정 추가

implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'

 

 

❗SwaggerConfig 작성

SwaggerConfig라는 설정클래스를 만들고 아래처럼 적어주면 된다

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket apiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .groupName("[그룹이름]")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.ant("/[requestMapping한 주소]/**")).build();
    }
    
}

 

❗서버 실행

서버를 실행하고 http://localhost:[포트번호]/swagger-ui.html에 접속하면 

이와같은 화면이 뜬다!
오른쪽 상단에서 groupName을 통해 설정한 그룹 이름을 볼 수 있다
원하는 그룹을 선택 후 컨트롤러 토글을 열어주면 api들이 자동으로 뜬다
해당 api의 response 정보를 예시로 보여주며, 위에 try it out을 누르고 execute를 누르면
해당 api를 실행했을 때의 응답값이 보이게 된다!

 

 

 

728x90
반응형
_니지